I tried the illustrate this by using supplots: However, I think managing the axes on subplots is quite difficult (e.g. how can one make the all y-axis between, for example [0 and 10]).
Boxplot (with several groups/days) + Plot (for each group/day)
1 view (last 30 days)
Show older comments
Dear All,
I need to plot several groups (g), for several different days (m) on a bar graph. I can do this with the following code:
Y = round(rand(m,g)*10);
bar(Y, 'group');
In addition to this, I want to plot a time-series to each group/day. The array which I want to add to this graph is also an array of size (m*g). This is illustrated if I had one day having 8 groups (i.e. m=1, g=8) at this address: http://www.mathworks.co.uk/help/matlab/creating_plots/bar-and-area-graphs.html, under the heading "Overlaying a Line Plot on the Bar Graph", but how we can do it if we have several days? I am thinking, I have to use plotyy at some point, but my several desperate attempts did not work?!
Lastly, could you also please help me to set the colors of each group?
Regards, Berk
Accepted Answer
Tom Lane
on 11 Oct 2012
Sorry, your frustration led me to try it myself. Here's what I did:
a = rand(3,4);
b = 10*rand(3,4);
figure(1); bar(a); % see what this looks like
figure(2); plot(b'); % see what this looks like
% now make a figure that looks like both
figure(3)
clf
h = bar(a);
hh = get(h,'Children');
hh = [hh{:}];
xd = get(hh,'XData');
xd = cat(3,xd{:});
m = squeeze(mean(xd))'; % x locations of bar centers
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),'YAxisLocation','right','Color','none');
hold on
hhh = plot(ax2,m,b');
hold off
linkaxes([ax1 ax2],'x')
0 Comments
More Answers (3)
Tom Lane
on 11 Oct 2012
Here's how you can set the color of the first group:
h = bar(Y, 'group');
set(h(1),'FaceColor','r')
If you are content with separate axes, here's how you can make sure all axes have the same y limits:
h1 = subplot(1,2,1); plot(rand(4,1))
h2 = subplot(1,2,2); plot(2*rand(4,1))
linkaxes([h1 h2],'y')
set(h1,'ylim',[0 2])
Otherwise, if you want to put stuff on the same graph, you'll need to determine the coordinates of each individual bar. Here's how to get started with that:
get(get(h(1),'Children'),'XData')
0 Comments
See Also
Categories
Find more on Two y-axis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!