并排执行不同的条形图
Enforcing different bar graphs side by side
我有 3 个一月份的温度值,一个二月份的,一个三月份的。我想把它们画在一个条形图上。我使用matlab中的叠加方法绘制了1月份的3个值。但是当我绘制其他 2 个月时,它们会覆盖 1 月。如何强制 2 月和 3 月的值与 1 月并列。
更新:我在下面的代码中添加了 运行 的输出,并进行了我想要的更改
temp_high = [12.5];
w1 = 0.5;
bar(x,temp_high,w1,'FaceColor',[0.2 0.2 0.5])
temp_low = [10.7];
w2 = .25;
hold on
bar(x,temp_low,w2,'FaceColor',[0 0.7 0.7])
temp_very_low = [7.1];
w2 = .1;
hold on
bar(x,temp_very_low,w2,'FaceColor',[0 0 0.7])
ax = gca;
ax.XTick = [1];
ax.XTickLabels = {'January'};
ax.XTickLabelRotation = 45;
name={'feb';'march'};
y=[5 ;
3 ]
bar_handle=bar(y);
set(gca, 'XTickLabel',name, 'XTick',1:numel(name))
ylabel('Temperature (\circF)')
legend({'jan 1-with 1-instance','jan 1-with 2-instance','jan 1-with 3-instance','feb', 'march'},'Location','northwest')
您的代码的主要问题在 bar(y)
。 y
中的两个值隐式绘制在 x 值 1 和 2 处。您想要的是,将它们绘制在 2 和 3 处。因此,您必须明确指定这些值。
我通过在变量中收集所有温度数据、宽度和颜色来冒昧 re-organize 你的代码。这样一来,所有 bar
图都可以在一个循环中完成。
代码如下:
figure(1);
hold on;
% Collect all data.
temp = [1 12.5; 1 10.7; 1 7.1; 2 5; 3 3];
w = [0.5 0.25 0.1 0.5 0.5];
c = [0.2 0.2 0.5; 0 0.7 0.7; 0 0 0.7; 1 0 0; 0 0 1];
% Plot all temperatures within single loop.
for ii = 1:numel(w)
bar(temp(ii, 1), temp(ii, 2), w(ii), 'FaceColor', c(ii, :));
end
% Decoration.
ticks = [1 2 3];
xlabels = {'January', 'February', 'March'};
set(gca, 'XTick', ticks, 'XTickLabel', xlabels);
ylabel('Temperature (\circF)');
legend({'jan 1-with 1-instance', 'jan 1-with 2-instance', 'jan 1-with 3-instance', 'feb', 'march'}, 'Location','northwest');
hold off;
我得到的输出如下所示:
希望对您有所帮助!
我有 3 个一月份的温度值,一个二月份的,一个三月份的。我想把它们画在一个条形图上。我使用matlab中的叠加方法绘制了1月份的3个值。但是当我绘制其他 2 个月时,它们会覆盖 1 月。如何强制 2 月和 3 月的值与 1 月并列。
更新:我在下面的代码中添加了 运行 的输出,并进行了我想要的更改
temp_high = [12.5];
w1 = 0.5;
bar(x,temp_high,w1,'FaceColor',[0.2 0.2 0.5])
temp_low = [10.7];
w2 = .25;
hold on
bar(x,temp_low,w2,'FaceColor',[0 0.7 0.7])
temp_very_low = [7.1];
w2 = .1;
hold on
bar(x,temp_very_low,w2,'FaceColor',[0 0 0.7])
ax = gca;
ax.XTick = [1];
ax.XTickLabels = {'January'};
ax.XTickLabelRotation = 45;
name={'feb';'march'};
y=[5 ;
3 ]
bar_handle=bar(y);
set(gca, 'XTickLabel',name, 'XTick',1:numel(name))
ylabel('Temperature (\circF)')
legend({'jan 1-with 1-instance','jan 1-with 2-instance','jan 1-with 3-instance','feb', 'march'},'Location','northwest')
您的代码的主要问题在 bar(y)
。 y
中的两个值隐式绘制在 x 值 1 和 2 处。您想要的是,将它们绘制在 2 和 3 处。因此,您必须明确指定这些值。
我通过在变量中收集所有温度数据、宽度和颜色来冒昧 re-organize 你的代码。这样一来,所有 bar
图都可以在一个循环中完成。
代码如下:
figure(1);
hold on;
% Collect all data.
temp = [1 12.5; 1 10.7; 1 7.1; 2 5; 3 3];
w = [0.5 0.25 0.1 0.5 0.5];
c = [0.2 0.2 0.5; 0 0.7 0.7; 0 0 0.7; 1 0 0; 0 0 1];
% Plot all temperatures within single loop.
for ii = 1:numel(w)
bar(temp(ii, 1), temp(ii, 2), w(ii), 'FaceColor', c(ii, :));
end
% Decoration.
ticks = [1 2 3];
xlabels = {'January', 'February', 'March'};
set(gca, 'XTick', ticks, 'XTickLabel', xlabels);
ylabel('Temperature (\circF)');
legend({'jan 1-with 1-instance', 'jan 1-with 2-instance', 'jan 1-with 3-instance', 'feb', 'march'}, 'Location','northwest');
hold off;
我得到的输出如下所示:
希望对您有所帮助!