bar graph with broken y-axis

39 次查看(过去 30 天)
Tim Gordon
Tim Gordon 2011-11-15
评论: Sebin 2023-8-16
Does anyone have code for creating a vertical bar graph with a broken y-axis?

回答(5 个)

Liam
Liam 2023-8-5
编辑:Liam 2023-8-5
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 个评论
Sebin
Sebin 2023-8-16
It works well and the resulting graph looks fancy!
Thank you so much!!

请先登录,再进行评论。


Tim Gordon
Tim Gordon 2011-12-10
Yes, I want to do what I asked about.

KAE
KAE 2022-6-1

Walter Roberson
Walter Roberson 2011-11-15
  1 个评论
Tim Gordon
Tim Gordon 2011-11-15
Nope. This only seems to work for scatter plots. I want a vertical bar plot.

请先登录,再进行评论。


Daniel Shub
Daniel Shub 2011-11-16
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.

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by