Search code examples
matlabplotbar-chartmatlab-figureaxis

Bar plot with two y axes


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

enter image description here


Solution

  • 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: enter image description here

    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!