create many bar plots with loop process

36 次查看(过去 30 天)
Hello!!
I need your help for an issue I had...
I have two matrixes
A --> 20x50 double
B -->20x50 double
and I want to create bar plots for the first rows of A and B, the secont bar plot will be the second rows of A and B etc... So I have to create 20 "grouped" bar plots from the respective rows of A and B
the x axis should be a cell array 1x50: 1, 2, 3, .... ,50+
I managed to create each bar plot without loop process but I was wondering how would be the code if I used a for loop to do all the bar plots ones and save them as a tif?
Thank you in advance

回答(2 个)

Scott MacKenzie
Scott MacKenzie 2021-5-12
编辑:Scott MacKenzie 2021-5-12
A = rand(20,50);
B = rand(20,50);
x = 1:50;
for i=1:size(A,1)
bar(x,[A(i,:); B(i,:)]);
filename = sprintf('plot_%02d.tif', i);
print(filename, '-dtiff');
end
You can't use a cell array for the x-axis. You can for the x-axis tick labels. Perhaps that's what you meant.
  6 个评论
Scott MacKenzie
Scott MacKenzie 2021-5-13
编辑:Scott MacKenzie 2021-5-13
Hmmm, that's odd. Seems the constraints on bar have changed since R2019a. The equivalent error message now is "the length of X must match the number of rows or columns of Y".
Try changing
x = 1:50;
to
x = 1:20;
Another thing to try is removing the x-argument from the bar function:
bar([A(i,:); B(i,:)]);
In this form, the x-argument is implicit. Good luck.

请先登录,再进行评论。


Adam Danz
Adam Danz 2021-5-13
编辑:Adam Danz 2021-5-13
If you want all 20 axes on the same figure, the bars will be quite tightly packed. Consider breaking up groups of axes into separate figures.
  • This uses TiledLayout to create the axes with compact spacing.
  • The edge lines of the bar are removed so you can see more of the bar colors.
  • The axes are all linked so their axis limits are all the same - this can be removed if you don't want to have identical axis limits for all axes.
% Create demo data
A = rand(20,50) .* linspace(1,10,50) + linspace(0,2,20)';
B = rand(20,50) .* linspace(1,12,50) + linspace(0,3,20)';
fig = figure();
tlo = tiledlayout(fig, 'flow','Padding','compact','TileSpacing','compact');
nPlots = size(A,1);
ax = gobjects(1,nPlots);
for i = 1:nPlots
ax(i) = nexttile(tlo);
bh = bar([A(i,:)', B(i,:)'],'Grouped','EdgeColor','none');
bh(1).FaceColor = 'r';
bh(2).FaceColor = 'b';
title(ax(i), sprintf('Row %d',i))
end
linkaxes(ax)
leg = legend(ax(end),{'A','B'},'Orientation','Horizontal');
leg.Layout.Tile = 'south';
Right-click and open the image in a new window to see full size.
Option 2: use connected scatter plot
This version uses vertical lines to connect the heights of each bar rather than showing the groupped bars. It therefore has more space between groups since the groups are vertically stacked. This demo uses a different random dataset than above.
A = rand(20,50) .* linspace(1,10,50) + linspace(0,2,20)';
B = rand(20,50) .* linspace(1,12,50) + linspace(0,3,20)';
fig = figure();
tlo = tiledlayout(fig, 'flow','Padding','compact','TileSpacing','compact');
nPlots = size(A,1);
ax = gobjects(1,nPlots);
x = 1:size(A,2);
for i = 1:nPlots
ax(i) = nexttile(tlo);
hold (ax(i), 'on')
plot([x;x],[A(i,:); B(i,:)], 'k-s','MarkerFaceColor','w','MarkerSize',4)
sh = scatter(x,[A(i,:); B(i,:)],16,[1 0 0; 0 0 1],'filled','MarkerFaceAlpha', .5,'MarkerEdgeColor', 'none','Marker', 's'); title(ax(i), sprintf('Row %d',i))
box(ax(i),'on')
axis(ax(i),'tight')
grid(ax(i),'on')
end
linkaxes(ax)
leg = legend(ax(end),sh,{'A','B'},'Orientation','Horizontal');
leg.Layout.Tile = 'south';
Right-click and open the image in a new window to see full size.

类别

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