Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comment
Berk
Berk on 10 Oct 2012
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]).

Sign in to comment.

Accepted Answer

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

More Answers (3)

Berk
Berk on 10 Oct 2012
Any help is greatly appreciated..
Thanks.

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

Berk
Berk on 11 Oct 2012
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.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!