Bart chart absolute width

3 次查看(过去 30 天)
Martijn Geelen
Martijn Geelen 2019-2-10
I create several bar charts during an optimization run in which i do not know the number of bars beforehand. I want all figures to be of the same size. The plots look quite nice, however, if there is only one bar in the figure, it creates a ugly bar chart. I would like to narrow the bar size in case there is one bar with probability 1 (happens quite often). In the attachment it is clearly visible what i mean.
Plot are created with the following code:
for i = 1:N_Locations
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
figure(4+ceil(i/9))
subplot(3,3,i-9*floor(i/9.01))
Rows = find(OrderSize_Empirical(:,1)==Locali(i));
bar(OrderSize_Empirical(Rows(1):Rows(end),3),'FaceColor', [45/255 0 84/255])
Ordersizes = num2cell(OrderSize_Empirical(Rows(1):Rows(end),2));
set(gca,'xticklabel',Ordersizes);
set(gca,'xtick',1:size(OrderSize_Empirical(Rows(1):Rows(end),2)));
str = strcat('Empirical order sizes at location: ',num2str(Locali(i)));
title(str);
xlabel('Order size')
ylabel('Probability')
end

回答(1 个)

Star Strider
Star Strider 2019-2-10
The bar plot widths appear to scale according to the number of bars plotted.
The easiest way might simply be to cheat:
if numel(OrderSize_Empirical(Rows(1):Rows(end),3)) <= 1
patch([-1 1 1 -1]*0.2, [0 0 1 1], [45/255 0 84/255])
xlim([-1 1])
set(gca, 'XTick',0, 'XTickLabel',1)
title(str);
xlabel('Order size')
ylabel('Probability')
end
Experiment to get the result you want.

类别

Help CenterFile Exchange 中查找有关 Bar Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by