create many bar plots with loop process

조회 수: 35 (최근 30일)
Giannis Nikolopoulos
Giannis Nikolopoulos 2021년 5월 12일
편집: Adam Danz 2021년 5월 13일
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
Giannis Nikolopoulos
Giannis Nikolopoulos 2021년 5월 13일
i used the Matlab 2019a
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