
Adding top x-axis using yyaxis for grouped bar graphs
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I am trying to add a second x-axis to my grouped bar graph, but on top. Applying both x-axes without manipulating tick mark locations gives what you see in the image. The bottom x-axis has the tick marks located at the center of each grouping, but the top x-axis has default tick mark locations not centered on the groups. It is hard to see, but there are two sets of tick marks on the top x-axis, one set coming from the default "bar" command that creates the bottom x-axis, and one set coming from the top x-axis I have created, but they are not aligned. I have tried everything I can to get the top x-axis tick mark locations (and labels) to match those of the bottom x-axis but have come up empty handed. Any help would be appreciated. My code for graphing is shown below as well. Thank you.


0 Commenti
Risposte (1)
dpb
il 2 Ago 2017
Modificato: dpb
il 2 Ago 2017
Set xlim and xtick values the same on the upper axis as the bottom. This can be done automagically by using linkaxes
If you look at your example, the x- axis limits are [1 24] but bar moves those to [0 25] to create some space around the end bar groups.
Also, when you create the axes, save the handles to the axes objects so you can address the one you need specifically -- when you get multiple axes in a figure, gca refers to the last one addressed and since you have more than one, having done something to one doesn't mean the next operation you wish to do is going to be on the last one which is what happens by default when specific handle is not provided.
Here's a similar example with R2014b so don't have the full-blown HG2 nor your data to duplicate but shows the idea...
data=abs(randn(11,12)); % some approximately similar data
hAx(1)=axes;
hAx(2)=axes('position',get(hAx,'position'),'color','none','XAxisLocation','top');
hBar=bar(hAx(1),data,'grouped');
colormap jet
hAx(2).XLim=hAx(1).XLim; % set the second axes limits
hAx(2).XTick=hAx(1).XTick; % same as first...
Above yields

which aligns the x ticks as wanted. Use linkaxes and if change one subsequently the other will reflect the change. Also set the y-axis values to match as well, of course.
There's no real difference in using yyaxis; in that case it creates two automagically but appears to not return their handles; you only get access via the 'left','right' argument. I presume if you do this, that you can then use 'XAxisLocation','top' to move the x-axis for it to the top rather than create yet another axes object as you've done above, but I can't test it here.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!