Boxplot (with several groups/days) + Plot (for each group/day)

1 次查看(过去 30 天)
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
  1 个评论
Berk
Berk 2012-10-10
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]).

请先登录,再进行评论。

采纳的回答

Tom Lane
Tom Lane 2012-10-11
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')

更多回答(3 个)

Berk
Berk 2012-10-10
Any help is greatly appreciated..
Thanks.

Tom Lane
Tom Lane 2012-10-11
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')

Berk
Berk 2012-10-11
Thanks Tom.
I managed to obtain the following figure after several hours of frustration. It uses subplots (5 supbplot, for each day), and assigns colors to each group as described by Haris in here http://www.mathworks.com/matlabcentral/answers/176. Then, I remove the x-axis, plot the second y-axis finally. I don't think that this solution is the neatest way, but it serves the purpose.

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by