bar graph with broken y-axis

29 views (last 30 days)
Tim Gordon
Tim Gordon on 15 Nov 2011
Commented: Sebin on 16 Aug 2023
Does anyone have code for creating a vertical bar graph with a broken y-axis?

Answers (5)

Liam
Liam on 5 Aug 2023
Edited: Liam on 5 Aug 2023
For anyone else trying to figure this out in the future, this can be accomplished with a tiled plot. Example:
% split bar graph - Liam Hurlburt (she/her) 2023.08.05
% input data here
barGraphData=[210 250 2 4 3 1; 275 225 3 4 5 2];
% set colors here
color1="#0A1F3E"; % very dark blue
color2="#1F4373"; % dark blue
color3="#2B95B7"; % electric blue
color4="#D1E4EC"; % light blue
color5="#931F32"; % red #1
color6="#B1272C"; % red #3
% set relative tile height here, higher numbers make
% top chart smaller. Minimum vaule = 2 (integers only)
tileRelativeHeight=3;
figure1 = figure; % Creates figure
t=tiledlayout(tileRelativeHeight,1); % creates tiled layout
% set Y axis label here
t.YLabel.String = 'y axis label';
%% top plot
ax1 = nexttile;
b=bar(barGraphData);
b(1).FaceColor = color1; % this section coordinates
b(2).FaceColor = color2; % colors in the top and
b(3).FaceColor = color3; % bottom plots, it can be
b(4).FaceColor = color4; % commented out to use
b(5).FaceColor = color5; % default colors
b(6).FaceColor = color6;
% uncomment line below to make make top plot logartigmic
% set(gca,'YScale','log')
set(gca,'TickLength',[0 0]) % removes tick marks and
set(gca,'Xtick',[]) % x-axis labels between plots
% legend labels go here
legend('data 1', 'data 2','data 3','data 4', 'data 5', 'data 6')
% set y limits for top plot here
ylim([200 300]);
%% bottom plot
ax2 = nexttile(2,[(tileRelativeHeight-1) 1]);
b=bar(barGraphData);
b(1).FaceColor = color1; % this section coordinates
b(2).FaceColor = color2; % colors in the top and
b(3).FaceColor = color3; % bottom plots, it can be
b(4).FaceColor = color4; % commented out to use
b(5).FaceColor = color5; % default colors
b(6).FaceColor = color6;
% x axis labels go here
set(gca,'xticklabel',["Series A","Series B"]);
set(gca,'TickLength',[0 0]) % coordinates axis tick styles between plots
% set y limits for bottom plot here
ylim([0 10]);
% adjust space between plots here, suggested
% values are 'tight' and 'none'
t.TileSpacing = 'tight';
  1 Comment
Sebin
Sebin on 16 Aug 2023
It works well and the resulting graph looks fancy!
Thank you so much!!

Sign in to comment.


Tim Gordon
Tim Gordon on 10 Dec 2011
Yes, I want to do what I asked about.

KAE
KAE on 1 Jun 2022

Walter Roberson
Walter Roberson on 15 Nov 2011
  1 Comment
Tim Gordon
Tim Gordon on 15 Nov 2011
Nope. This only seems to work for scatter plots. I want a vertical bar plot.

Sign in to comment.


Daniel Shub
Daniel Shub on 16 Nov 2011
Are you sure you want to do this? In my opinion this rarely looks good and also is usually not the best way to convey information. What about an insert plot showing the small values? You cannot do this with standard MATLAB. You could play around with placing patches over the bars, but it is not going to look professional. I think you can do this with SigmaPlot.

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!