条形图有两个y轴

Len*_*naH 2 matlab plot axis bar-chart matlab-figure

我有以下代码用于在MATLAB中使用2个y轴的绘图.我很高兴双轴功能有效,但是,我想避免条形图的重叠.此外,右侧轴上的类别应该具有不同的颜色,不仅是黄色,而且应该以某种方式清楚它们是在右侧轴而不是左侧轴上绘制的.如何才能做到这一点?

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')  
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Dev*_*-iL 7

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

我改变的主要是添加隐形条(使用NaN),以便颜色顺序保持不变:

function q54071610
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].' NaN(3,4)];
values2 = [NaN(3,2) [GermanByEON; GermanByRWE; EONGermanPortfolio; RWEGermanPortfolio].'];
years1 = repmat(years.', [1,6]);
figure;
bar(years1,values1, 'EdgeColor', 'b', 'LineWidth', 2);
ylabel('Utilities generation portfolio in MW')  
yyaxis right
hB = bar(years1,values2, 'EdgeColor', 'r', 'LineWidth', 2);
c = lines(6);
for ind1 = 1:6
  hB(ind1).FaceColor = c(ind1, :);
end
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')
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述

现在您应该解释红框条纹属于右轴,而蓝框条形条属于左侧.