필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Berk
Berk 2012년 10월 10일
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에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by