带两个y轴的条形图 [英] Bar plot with two y axes

查看:92
本文介绍了带两个y轴的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,对于具有2个y轴的绘图,我具有以下代码.我很高兴2轴功能起作用,但是,我想避免条形重叠.同样,右侧轴上的类别应该具有不同的颜色,不仅是黄色,而且应该以某种方式清楚地将它们绘制在右侧轴上,而不是左侧轴上.该怎么办?

EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014];
% Plot
values1 = [EONMW; RWEMW]';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio]';
years1 = [years; years]';
years2 = [years; years; years; years]';
figure;
bar(years1,values1);
ylabel('Utilities generation portfolio in MW')  
yyaxis right
bar(years2,values2);
legend('EON German portfolio in MW', 'RWE German portfolio in MW',...
    'Percentage of German portfolio by EON', 'Percentage of German portfolio by RWE',...
    'EON"s percentage of generation in Germany', 'RWE"s percentage of generation in Germany')
legend('Location','northwest')
ylabel('Utilities generation portfolio as percentages')  

解决方案

我不确定这些条形到底是什么意思,因此我可能错过了图中的要点(这可能是这里的主要问题).但是,我发现这种表示方式并不令人愉悦和误导,因为读者需要花费大量精力来了解哪个价值属于哪个标准,什么是可比较的,什么不是.

我在这里提出的建议不是对技术问题(您已经从@ Dev-iL获得的)的直接答案,而是针对更基本问题的另一种解决方案-如何可视化这些数据?我相信,如果我能理解数字所代表的含义(百分比是多少?),以及您想在此图中强调什么,那么我可以找到一个更好的解决方案.

首先,代码:

EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014].';
values1 = [EONMW; RWEMW].';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio].'*100;

% Plot
colMap = mat2cell(lines(2),[1 1],3); % Choose your favorite colors
figure(2);
% upper plot:
subplot 211
b = bar(years,values1);
set(b,{'FaceColor'},colMap)
xticklabels({}) % remove the years labels, the bottom axes will show them
ylabel('Utilities generation portfolio in MW')
legend('EON German', 'RWE German',...
    'Location','northwest')

% bottom plot
subplot 212
b = bar(years,values2);
set(b,{'FaceColor'},repmat(colMap,2,1)) % matching the colors by topic
set(b,{'FaceAlpha'},{1;1;0.6;0.6}) % distiguish between related mesures
xlabel('Year')
ylabel('Utilities generation portfolio (%)')
legend('German portfolio by EON', 'German portfolio by RWE',...
    'EON''s generation in Germany', 'RWE''s generation in Germany',...
    'Location','north')

结果:

我更改的主要内容:

  1. 以y轴为单位分割条形,但以x轴为单位对齐
  2. 在各图之间匹配相关条的颜色
  3. 缩图例和标签

祝你好运!

I have the following code for a plot with 2 y-axes in MATLAB. I am glad that the 2-axes feature works, however, I would like to avoid the overlapping of the bars. Also, the categories on the right-hand axis should have different colors, not only yellow, yet it should be somehow clear that they are plotted on the right-hand axis and not the left one. How can this be done?

EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014];
% Plot
values1 = [EONMW; RWEMW]';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio]';
years1 = [years; years]';
years2 = [years; years; years; years]';
figure;
bar(years1,values1);
ylabel('Utilities generation portfolio in MW')  
yyaxis right
bar(years2,values2);
legend('EON German portfolio in MW', 'RWE German portfolio in MW',...
    'Percentage of German portfolio by EON', 'Percentage of German portfolio by RWE',...
    'EON"s percentage of generation in Germany', 'RWE"s percentage of generation in Germany')
legend('Location','northwest')
ylabel('Utilities generation portfolio as percentages')  

解决方案

I'm not sure what exactly these bars mean, and so I may have missed the point of the figure (which could be the main problem here). However, I find this way of presentation not pleasing and misleading, as it takes a lot of effort from the reader to understand which value belongs to which bar, and what is comparable and what's not.

What I suggest here, is not a direct answer to the technical problem (which you have already got from @Dev-iL), but a different solution for the more basic problem - how to visualize these data? I believe that if I'll understand what the numbers represent (percentage from what?) and what you want to emphasize with this plot, I can find a better solution.

First, the code:

EONMW = [100 399 500];
RWEMW = [200 996 120];
GermanByEON = [0.2 0.4 0.5];
GermanByRWE = [0.1 0.5 0.9];
EONGermanPortfolio = [0.7 0.2 0.1];
RWEGermanPortfolio = [0.8 0.3 0.6];
years = [2010 2012 2014].';
values1 = [EONMW; RWEMW].';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio].'*100;

% Plot
colMap = mat2cell(lines(2),[1 1],3); % Choose your favorite colors
figure(2);
% upper plot:
subplot 211
b = bar(years,values1);
set(b,{'FaceColor'},colMap)
xticklabels({}) % remove the years labels, the bottom axes will show them
ylabel('Utilities generation portfolio in MW')
legend('EON German', 'RWE German',...
    'Location','northwest')

% bottom plot
subplot 212
b = bar(years,values2);
set(b,{'FaceColor'},repmat(colMap,2,1)) % matching the colors by topic
set(b,{'FaceAlpha'},{1;1;0.6;0.6}) % distiguish between related mesures
xlabel('Year')
ylabel('Utilities generation portfolio (%)')
legend('German portfolio by EON', 'German portfolio by RWE',...
    'EON''s generation in Germany', 'RWE''s generation in Germany',...
    'Location','north')

The result:

The major things I changed:

  1. Split the bars by the units of the y-axis, but align them by the x-axis
  2. Match the colors of related bars between the plots
  3. Shorten legends and labels

Good luck!

这篇关于带两个y轴的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆