Bar plot x-axis Matlab












0















I am stuck with a bar plot in Matlab. I got it to work with Matlab help and this this forum until here, but on the x-axis, there are still only 2 names. I would like to have the "names" under the bars and the "categories" where the 2 names show up now. Thank you!



values = [4 10...
11 2 3;...
4 1...
5 2 -10];
names = {'PreSplitTotalEON' 'PostSplitTotalEON'...
'PreSplitPureEON' 'PostSplitPureEON' 'PostSplitUniper';...
'PreSplitTotalRWE' 'PostSplitTotalRWE'...
'PreSplitPureRWE' 'PostSplitPureRWE' 'PostSplitInnogy'};
categories = {'EON', 'RWE'};
b = bar(values,'FaceColor','flat');
xticklabels([names(1,:)';names(2,:)'])
% This will set labels to be used for each tick of the x-axis
xticks(1:1:length([names(1,:)';names(2,:)']))
% This will set how many ticks you want on the x-axis. Here, there
% should be 48 ticks being generated. One for each piece of data you have.
xtickangle(90)
% This will rotate the label so that the labels will not overlap
% with one another. This is in degrees.
for k = 1:size(values,2) % for fancier colors.
b(k).CData = k;
end









share|improve this question



























    0















    I am stuck with a bar plot in Matlab. I got it to work with Matlab help and this this forum until here, but on the x-axis, there are still only 2 names. I would like to have the "names" under the bars and the "categories" where the 2 names show up now. Thank you!



    values = [4 10...
    11 2 3;...
    4 1...
    5 2 -10];
    names = {'PreSplitTotalEON' 'PostSplitTotalEON'...
    'PreSplitPureEON' 'PostSplitPureEON' 'PostSplitUniper';...
    'PreSplitTotalRWE' 'PostSplitTotalRWE'...
    'PreSplitPureRWE' 'PostSplitPureRWE' 'PostSplitInnogy'};
    categories = {'EON', 'RWE'};
    b = bar(values,'FaceColor','flat');
    xticklabels([names(1,:)';names(2,:)'])
    % This will set labels to be used for each tick of the x-axis
    xticks(1:1:length([names(1,:)';names(2,:)']))
    % This will set how many ticks you want on the x-axis. Here, there
    % should be 48 ticks being generated. One for each piece of data you have.
    xtickangle(90)
    % This will rotate the label so that the labels will not overlap
    % with one another. This is in degrees.
    for k = 1:size(values,2) % for fancier colors.
    b(k).CData = k;
    end









    share|improve this question

























      0












      0








      0


      1






      I am stuck with a bar plot in Matlab. I got it to work with Matlab help and this this forum until here, but on the x-axis, there are still only 2 names. I would like to have the "names" under the bars and the "categories" where the 2 names show up now. Thank you!



      values = [4 10...
      11 2 3;...
      4 1...
      5 2 -10];
      names = {'PreSplitTotalEON' 'PostSplitTotalEON'...
      'PreSplitPureEON' 'PostSplitPureEON' 'PostSplitUniper';...
      'PreSplitTotalRWE' 'PostSplitTotalRWE'...
      'PreSplitPureRWE' 'PostSplitPureRWE' 'PostSplitInnogy'};
      categories = {'EON', 'RWE'};
      b = bar(values,'FaceColor','flat');
      xticklabels([names(1,:)';names(2,:)'])
      % This will set labels to be used for each tick of the x-axis
      xticks(1:1:length([names(1,:)';names(2,:)']))
      % This will set how many ticks you want on the x-axis. Here, there
      % should be 48 ticks being generated. One for each piece of data you have.
      xtickangle(90)
      % This will rotate the label so that the labels will not overlap
      % with one another. This is in degrees.
      for k = 1:size(values,2) % for fancier colors.
      b(k).CData = k;
      end









      share|improve this question














      I am stuck with a bar plot in Matlab. I got it to work with Matlab help and this this forum until here, but on the x-axis, there are still only 2 names. I would like to have the "names" under the bars and the "categories" where the 2 names show up now. Thank you!



      values = [4 10...
      11 2 3;...
      4 1...
      5 2 -10];
      names = {'PreSplitTotalEON' 'PostSplitTotalEON'...
      'PreSplitPureEON' 'PostSplitPureEON' 'PostSplitUniper';...
      'PreSplitTotalRWE' 'PostSplitTotalRWE'...
      'PreSplitPureRWE' 'PostSplitPureRWE' 'PostSplitInnogy'};
      categories = {'EON', 'RWE'};
      b = bar(values,'FaceColor','flat');
      xticklabels([names(1,:)';names(2,:)'])
      % This will set labels to be used for each tick of the x-axis
      xticks(1:1:length([names(1,:)';names(2,:)']))
      % This will set how many ticks you want on the x-axis. Here, there
      % should be 48 ticks being generated. One for each piece of data you have.
      xtickangle(90)
      % This will rotate the label so that the labels will not overlap
      % with one another. This is in degrees.
      for k = 1:size(values,2) % for fancier colors.
      b(k).CData = k;
      end






      matlab plot matlab-figure






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 14:11









      LenaHLenaH

      1528




      1528
























          3 Answers
          3






          active

          oldest

          votes


















          2














          The default way would be to use legend to display the name of each element in the group. But the position of each bar can be accessed through the XOffset and XData properties. See this answer in matlab central.



          So you can use something like:



          ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
          xticks(ticksList(:))
          xticklabels([names(1,:)';names(2,:)'])


          to correctly display the names below each bar. However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. You could instead display the categories on top by creating a new axes. Adding Something like:



          ax1 = gca;
          ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
          set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
          set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
          xticks(b(1).XData)
          xticklabels(categories)


          I.e. the complete code would be now:



          clear all
          close all

          values = [4 1 11 2 3; 4 1 5 2 -10];
          names = {'Pre split total EON' 'Post split total EON'...
          'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
          'Pre split total RWE' 'Post split total RWE'...
          'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
          categories = {'EON','RWE'};
          figure;
          b = bar(values,'FaceColor','flat');
          ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
          xticks(ticksList(:))
          xticklabels([names(1,:)';names(2,:)'])
          xtickangle(90)
          ax1 = gca;
          ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
          set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
          set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
          set(ax2, 'YTick', );
          xticks(b(1).XData)
          xticklabels(categories)
          for k = 1:size(values,2) % for fancier colors.
          b(k).CData = k;
          end


          Figure output






          share|improve this answer


























          • I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

            – LenaH
            Dec 31 '18 at 8:02











          • Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

            – LenaH
            Jan 1 at 9:32











          • Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

            – Javier Orlandi
            Jan 1 at 11:59



















          1















          • You need to observe the x-axis from the graph, and approximate the
            start and end x-axis values of the bars.

          • On the inspection it was found that bars start from 0.54 (the gap) at
            the x-axis and ends near 2.32.

          • Next, divide the x-axis into 12 tick positions using the command xticks,

          • and then mark those position with the required labels using the command xticklabels. That's all.


          The required code to do so is given below.



          close all
          clear all

          values = [4 10 ...
          11 2 3; ...
          4 1 ...
          5 2 -10];

          % just declare the names lables as a simple 1-D cell array
          % remove the columns and construct as row wise cell array
          names = {'PreSplitTotalEON','PostSplitTotalEON', ...
          'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
          'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
          'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
          % declare the categories label
          categories = {'EON', 'RWE'};
          % construct the bar
          b = bar(values,'FaceColor','flat');

          %-> mark the respective xticks <-
          % on close inspepection, it was found that the bars starts
          % near 0.54 and end at nearly 2.31 at the x-axis
          % so divide the x axis in 12 ticks within those limits.
          xticks([linspace(0.54, 2.31, 12)]);

          % now put the lables at those limits, that all
          xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})

          xtickangle(90)
          % This will rotate the label so that the labels will not overlap
          % with one another. This is in degrees.
          for k = 1:5 % for fancier colors.
          b(k).CData = k;
          end


          Output
          enter image description here






          share|improve this answer

































            1














            Here is a fix for your code to get the desired output (explenations inside):



            values = [4 10 11 2 3;
            4 1 5 2 -10];
            names = {'PreSplitTotal' 'PostSplitTotal'...
            'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
            'PreSplitTotal' 'PostSplitTotal'...
            'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
            categories = {'EON', 'RWE'};
            N_names = size(values,2);
            ax = axes('NextPlot','add'); % <-- same as 'hold on'
            col = lines(N_names); % choose your favorite colormap
            % draw the bars in pairs by their 'names':
            for k = 1:N_names % for fancier colors.categories
            b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
            'FaceColor',col(k,:));
            end
            xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
            xticklabels(names(:))
            xtickangle(30)
            % place the category name on top of the axes:
            text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
            'VerticalAlignment','bottom','HorizontalAlignment','center')


            enter image description here






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978309%2fbar-plot-x-axis-matlab%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              The default way would be to use legend to display the name of each element in the group. But the position of each bar can be accessed through the XOffset and XData properties. See this answer in matlab central.



              So you can use something like:



              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])


              to correctly display the names below each bar. However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. You could instead display the categories on top by creating a new axes. Adding Something like:



              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              xticks(b(1).XData)
              xticklabels(categories)


              I.e. the complete code would be now:



              clear all
              close all

              values = [4 1 11 2 3; 4 1 5 2 -10];
              names = {'Pre split total EON' 'Post split total EON'...
              'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
              'Pre split total RWE' 'Post split total RWE'...
              'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
              categories = {'EON','RWE'};
              figure;
              b = bar(values,'FaceColor','flat');
              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])
              xtickangle(90)
              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              set(ax2, 'YTick', );
              xticks(b(1).XData)
              xticklabels(categories)
              for k = 1:size(values,2) % for fancier colors.
              b(k).CData = k;
              end


              Figure output






              share|improve this answer


























              • I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

                – LenaH
                Dec 31 '18 at 8:02











              • Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

                – LenaH
                Jan 1 at 9:32











              • Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

                – Javier Orlandi
                Jan 1 at 11:59
















              2














              The default way would be to use legend to display the name of each element in the group. But the position of each bar can be accessed through the XOffset and XData properties. See this answer in matlab central.



              So you can use something like:



              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])


              to correctly display the names below each bar. However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. You could instead display the categories on top by creating a new axes. Adding Something like:



              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              xticks(b(1).XData)
              xticklabels(categories)


              I.e. the complete code would be now:



              clear all
              close all

              values = [4 1 11 2 3; 4 1 5 2 -10];
              names = {'Pre split total EON' 'Post split total EON'...
              'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
              'Pre split total RWE' 'Post split total RWE'...
              'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
              categories = {'EON','RWE'};
              figure;
              b = bar(values,'FaceColor','flat');
              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])
              xtickangle(90)
              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              set(ax2, 'YTick', );
              xticks(b(1).XData)
              xticklabels(categories)
              for k = 1:size(values,2) % for fancier colors.
              b(k).CData = k;
              end


              Figure output






              share|improve this answer


























              • I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

                – LenaH
                Dec 31 '18 at 8:02











              • Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

                – LenaH
                Jan 1 at 9:32











              • Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

                – Javier Orlandi
                Jan 1 at 11:59














              2












              2








              2







              The default way would be to use legend to display the name of each element in the group. But the position of each bar can be accessed through the XOffset and XData properties. See this answer in matlab central.



              So you can use something like:



              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])


              to correctly display the names below each bar. However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. You could instead display the categories on top by creating a new axes. Adding Something like:



              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              xticks(b(1).XData)
              xticklabels(categories)


              I.e. the complete code would be now:



              clear all
              close all

              values = [4 1 11 2 3; 4 1 5 2 -10];
              names = {'Pre split total EON' 'Post split total EON'...
              'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
              'Pre split total RWE' 'Post split total RWE'...
              'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
              categories = {'EON','RWE'};
              figure;
              b = bar(values,'FaceColor','flat');
              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])
              xtickangle(90)
              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              set(ax2, 'YTick', );
              xticks(b(1).XData)
              xticklabels(categories)
              for k = 1:size(values,2) % for fancier colors.
              b(k).CData = k;
              end


              Figure output






              share|improve this answer















              The default way would be to use legend to display the name of each element in the group. But the position of each bar can be accessed through the XOffset and XData properties. See this answer in matlab central.



              So you can use something like:



              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])


              to correctly display the names below each bar. However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. You could instead display the categories on top by creating a new axes. Adding Something like:



              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              xticks(b(1).XData)
              xticklabels(categories)


              I.e. the complete code would be now:



              clear all
              close all

              values = [4 1 11 2 3; 4 1 5 2 -10];
              names = {'Pre split total EON' 'Post split total EON'...
              'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
              'Pre split total RWE' 'Post split total RWE'...
              'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
              categories = {'EON','RWE'};
              figure;
              b = bar(values,'FaceColor','flat');
              ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
              xticks(ticksList(:))
              xticklabels([names(1,:)';names(2,:)'])
              xtickangle(90)
              ax1 = gca;
              ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
              set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
              set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
              set(ax2, 'YTick', );
              xticks(b(1).XData)
              xticklabels(categories)
              for k = 1:size(values,2) % for fancier colors.
              b(k).CData = k;
              end


              Figure output







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 1 at 11:57

























              answered Dec 30 '18 at 17:12









              Javier OrlandiJavier Orlandi

              814




              814













              • I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

                – LenaH
                Dec 31 '18 at 8:02











              • Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

                – LenaH
                Jan 1 at 9:32











              • Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

                – Javier Orlandi
                Jan 1 at 11:59



















              • I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

                – LenaH
                Dec 31 '18 at 8:02











              • Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

                – LenaH
                Jan 1 at 9:32











              • Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

                – Javier Orlandi
                Jan 1 at 11:59

















              I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

              – LenaH
              Dec 31 '18 at 8:02





              I like the solution with the categories above, very nice, thank you! However, there is now a problem with the right hand y-axis. It is somehow different than the left hand y-axis. I will suggest an edit to your reply with the complete code that you suggested, and you could try to resolve the problem maybe? Thanks.

              – LenaH
              Dec 31 '18 at 8:02













              Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

              – LenaH
              Jan 1 at 9:32





              Thank you. The problem with the right hand y-axis is still there, though, when I enlarge or shrink the figure... Do you know what I mean?

              – LenaH
              Jan 1 at 9:32













              Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

              – Javier Orlandi
              Jan 1 at 11:59





              Should be fixed now by disabling the YTick on the second axes. The other option would be to use linkaxes, so they have the same range. But with linkaxes sometimes after zooming the axes might not match perfectly. Another option would be to just use the text function like in @EBH answer

              – Javier Orlandi
              Jan 1 at 11:59













              1















              • You need to observe the x-axis from the graph, and approximate the
                start and end x-axis values of the bars.

              • On the inspection it was found that bars start from 0.54 (the gap) at
                the x-axis and ends near 2.32.

              • Next, divide the x-axis into 12 tick positions using the command xticks,

              • and then mark those position with the required labels using the command xticklabels. That's all.


              The required code to do so is given below.



              close all
              clear all

              values = [4 10 ...
              11 2 3; ...
              4 1 ...
              5 2 -10];

              % just declare the names lables as a simple 1-D cell array
              % remove the columns and construct as row wise cell array
              names = {'PreSplitTotalEON','PostSplitTotalEON', ...
              'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
              'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
              'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
              % declare the categories label
              categories = {'EON', 'RWE'};
              % construct the bar
              b = bar(values,'FaceColor','flat');

              %-> mark the respective xticks <-
              % on close inspepection, it was found that the bars starts
              % near 0.54 and end at nearly 2.31 at the x-axis
              % so divide the x axis in 12 ticks within those limits.
              xticks([linspace(0.54, 2.31, 12)]);

              % now put the lables at those limits, that all
              xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})

              xtickangle(90)
              % This will rotate the label so that the labels will not overlap
              % with one another. This is in degrees.
              for k = 1:5 % for fancier colors.
              b(k).CData = k;
              end


              Output
              enter image description here






              share|improve this answer






























                1















                • You need to observe the x-axis from the graph, and approximate the
                  start and end x-axis values of the bars.

                • On the inspection it was found that bars start from 0.54 (the gap) at
                  the x-axis and ends near 2.32.

                • Next, divide the x-axis into 12 tick positions using the command xticks,

                • and then mark those position with the required labels using the command xticklabels. That's all.


                The required code to do so is given below.



                close all
                clear all

                values = [4 10 ...
                11 2 3; ...
                4 1 ...
                5 2 -10];

                % just declare the names lables as a simple 1-D cell array
                % remove the columns and construct as row wise cell array
                names = {'PreSplitTotalEON','PostSplitTotalEON', ...
                'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
                'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
                'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
                % declare the categories label
                categories = {'EON', 'RWE'};
                % construct the bar
                b = bar(values,'FaceColor','flat');

                %-> mark the respective xticks <-
                % on close inspepection, it was found that the bars starts
                % near 0.54 and end at nearly 2.31 at the x-axis
                % so divide the x axis in 12 ticks within those limits.
                xticks([linspace(0.54, 2.31, 12)]);

                % now put the lables at those limits, that all
                xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})

                xtickangle(90)
                % This will rotate the label so that the labels will not overlap
                % with one another. This is in degrees.
                for k = 1:5 % for fancier colors.
                b(k).CData = k;
                end


                Output
                enter image description here






                share|improve this answer




























                  1












                  1








                  1








                  • You need to observe the x-axis from the graph, and approximate the
                    start and end x-axis values of the bars.

                  • On the inspection it was found that bars start from 0.54 (the gap) at
                    the x-axis and ends near 2.32.

                  • Next, divide the x-axis into 12 tick positions using the command xticks,

                  • and then mark those position with the required labels using the command xticklabels. That's all.


                  The required code to do so is given below.



                  close all
                  clear all

                  values = [4 10 ...
                  11 2 3; ...
                  4 1 ...
                  5 2 -10];

                  % just declare the names lables as a simple 1-D cell array
                  % remove the columns and construct as row wise cell array
                  names = {'PreSplitTotalEON','PostSplitTotalEON', ...
                  'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
                  'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
                  'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
                  % declare the categories label
                  categories = {'EON', 'RWE'};
                  % construct the bar
                  b = bar(values,'FaceColor','flat');

                  %-> mark the respective xticks <-
                  % on close inspepection, it was found that the bars starts
                  % near 0.54 and end at nearly 2.31 at the x-axis
                  % so divide the x axis in 12 ticks within those limits.
                  xticks([linspace(0.54, 2.31, 12)]);

                  % now put the lables at those limits, that all
                  xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})

                  xtickangle(90)
                  % This will rotate the label so that the labels will not overlap
                  % with one another. This is in degrees.
                  for k = 1:5 % for fancier colors.
                  b(k).CData = k;
                  end


                  Output
                  enter image description here






                  share|improve this answer
















                  • You need to observe the x-axis from the graph, and approximate the
                    start and end x-axis values of the bars.

                  • On the inspection it was found that bars start from 0.54 (the gap) at
                    the x-axis and ends near 2.32.

                  • Next, divide the x-axis into 12 tick positions using the command xticks,

                  • and then mark those position with the required labels using the command xticklabels. That's all.


                  The required code to do so is given below.



                  close all
                  clear all

                  values = [4 10 ...
                  11 2 3; ...
                  4 1 ...
                  5 2 -10];

                  % just declare the names lables as a simple 1-D cell array
                  % remove the columns and construct as row wise cell array
                  names = {'PreSplitTotalEON','PostSplitTotalEON', ...
                  'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
                  'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
                  'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
                  % declare the categories label
                  categories = {'EON', 'RWE'};
                  % construct the bar
                  b = bar(values,'FaceColor','flat');

                  %-> mark the respective xticks <-
                  % on close inspepection, it was found that the bars starts
                  % near 0.54 and end at nearly 2.31 at the x-axis
                  % so divide the x axis in 12 ticks within those limits.
                  xticks([linspace(0.54, 2.31, 12)]);

                  % now put the lables at those limits, that all
                  xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})

                  xtickangle(90)
                  % This will rotate the label so that the labels will not overlap
                  % with one another. This is in degrees.
                  for k = 1:5 % for fancier colors.
                  b(k).CData = k;
                  end


                  Output
                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 31 '18 at 5:12

























                  answered Dec 31 '18 at 5:05









                  AnjanAnjan

                  33917




                  33917























                      1














                      Here is a fix for your code to get the desired output (explenations inside):



                      values = [4 10 11 2 3;
                      4 1 5 2 -10];
                      names = {'PreSplitTotal' 'PostSplitTotal'...
                      'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
                      'PreSplitTotal' 'PostSplitTotal'...
                      'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
                      categories = {'EON', 'RWE'};
                      N_names = size(values,2);
                      ax = axes('NextPlot','add'); % <-- same as 'hold on'
                      col = lines(N_names); % choose your favorite colormap
                      % draw the bars in pairs by their 'names':
                      for k = 1:N_names % for fancier colors.categories
                      b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
                      'FaceColor',col(k,:));
                      end
                      xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
                      xticklabels(names(:))
                      xtickangle(30)
                      % place the category name on top of the axes:
                      text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
                      'VerticalAlignment','bottom','HorizontalAlignment','center')


                      enter image description here






                      share|improve this answer




























                        1














                        Here is a fix for your code to get the desired output (explenations inside):



                        values = [4 10 11 2 3;
                        4 1 5 2 -10];
                        names = {'PreSplitTotal' 'PostSplitTotal'...
                        'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
                        'PreSplitTotal' 'PostSplitTotal'...
                        'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
                        categories = {'EON', 'RWE'};
                        N_names = size(values,2);
                        ax = axes('NextPlot','add'); % <-- same as 'hold on'
                        col = lines(N_names); % choose your favorite colormap
                        % draw the bars in pairs by their 'names':
                        for k = 1:N_names % for fancier colors.categories
                        b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
                        'FaceColor',col(k,:));
                        end
                        xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
                        xticklabels(names(:))
                        xtickangle(30)
                        % place the category name on top of the axes:
                        text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
                        'VerticalAlignment','bottom','HorizontalAlignment','center')


                        enter image description here






                        share|improve this answer


























                          1












                          1








                          1







                          Here is a fix for your code to get the desired output (explenations inside):



                          values = [4 10 11 2 3;
                          4 1 5 2 -10];
                          names = {'PreSplitTotal' 'PostSplitTotal'...
                          'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
                          'PreSplitTotal' 'PostSplitTotal'...
                          'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
                          categories = {'EON', 'RWE'};
                          N_names = size(values,2);
                          ax = axes('NextPlot','add'); % <-- same as 'hold on'
                          col = lines(N_names); % choose your favorite colormap
                          % draw the bars in pairs by their 'names':
                          for k = 1:N_names % for fancier colors.categories
                          b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
                          'FaceColor',col(k,:));
                          end
                          xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
                          xticklabels(names(:))
                          xtickangle(30)
                          % place the category name on top of the axes:
                          text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
                          'VerticalAlignment','bottom','HorizontalAlignment','center')


                          enter image description here






                          share|improve this answer













                          Here is a fix for your code to get the desired output (explenations inside):



                          values = [4 10 11 2 3;
                          4 1 5 2 -10];
                          names = {'PreSplitTotal' 'PostSplitTotal'...
                          'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
                          'PreSplitTotal' 'PostSplitTotal'...
                          'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
                          categories = {'EON', 'RWE'};
                          N_names = size(values,2);
                          ax = axes('NextPlot','add'); % <-- same as 'hold on'
                          col = lines(N_names); % choose your favorite colormap
                          % draw the bars in pairs by their 'names':
                          for k = 1:N_names % for fancier colors.categories
                          b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
                          'FaceColor',col(k,:));
                          end
                          xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
                          xticklabels(names(:))
                          xtickangle(30)
                          % place the category name on top of the axes:
                          text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
                          'VerticalAlignment','bottom','HorizontalAlignment','center')


                          enter image description here







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 31 '18 at 23:06









                          EBHEBH

                          9,44332247




                          9,44332247






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978309%2fbar-plot-x-axis-matlab%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Mossoró

                              Error while reading .h5 file using the rhdf5 package in R

                              Pushsharp Apns notification error: 'InvalidToken'