带两个y轴的条形图

时间:2019-01-07 09:26:44

标签: matlab plot bar-chart matlab-figure axis

对于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')  

enter image description here

3 个答案:

答案 0 :(得分:7)

我同意阅读这种情节有点困难,但是也许有一种方法可以稍微改善这种情况。

我更改的主要内容是添加了不可见的条(使用select X.id as prod_id1, minprice, Y.id as prod_id2, maxprice from ( select id, price as minprice from tablename where price =(select min(price) from tablename b) )X cross join ( select id, price as maxprice from tablename where price =(select max(price) from tablename c) )Y ),以便颜色顺序保持不变:

NaN

这将导致:

enter image description here

现在,您应该解释一下,红色边框属于右侧,而蓝色边框属于左侧。

答案 1 :(得分:1)

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

我在这里提出的建议不是对技术问题(您已经从@ 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')

结果: enter image description here

我更改的主要内容:

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

祝你好运!

答案 2 :(得分:0)

最好将bar(x,y)定义为b=bar(x,y),然后控制b的选项,其中之一是b.FaceColor。看一下下面的代码

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];
x=0.2;
% Plot
values1 = [EONMW; RWEMW]';
values2 = [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio]';
years1 = [years; years]';
years2 = [years; years; years; years]';
figure;
b1=bar(years1,values1,x*0.66);
ylabel('Utilities generation portfolio in MW')  
yyaxis right
b2=bar(years2,values2,x);
%%%%%%%%%%%%
%%%%%%%%%%%%
%% Defining colors
b1(1).FaceColor=[1 0 0];
b1(2).FaceColor=[0 1 0];
b2(1).FaceColor=[0 0 1];
b2(2).FaceColor=[1 1 0];
b2(3).FaceColor=[0 1 1];
b2(4).FaceColor=[1 0 1];
%%%%%%%%%%%%
%%%%%%%%%%%%
%%
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')  

这是您的代码加上以下代码块

%%%%%%%%%%%%
%%%%%%%%%%%%
%% Defining colors
b1(1).FaceColor=[1 0 0];
b1(2).FaceColor=[0 1 0];
b2(1).FaceColor=[0 0 1];
b2(2).FaceColor=[1 1 0];
b2(3).FaceColor=[0 1 1];
b2(4).FaceColor=[1 0 1];
%%%%%%%%%%%%
%%%%%%%%%%%%
%%

结果如下

enter image description here

还添加了一个触发选项x以控制条形宽度