bar graph with broken y-axis

41 vues (au cours des 30 derniers jours)
Tim Gordon
Tim Gordon le 15 Nov 2011
Commenté : Sebin le 16 Août 2023
Does anyone have code for creating a vertical bar graph with a broken y-axis?

Réponses (5)

Liam
Liam le 5 Août 2023
Modifié(e) : Liam le 5 Août 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 commentaire
Sebin
Sebin le 16 Août 2023
It works well and the resulting graph looks fancy!
Thank you so much!!

Connectez-vous pour commenter.


Tim Gordon
Tim Gordon le 10 Déc 2011
Yes, I want to do what I asked about.

KAE
KAE le 1 Juin 2022

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

Connectez-vous pour commenter.


Daniel Shub
Daniel Shub le 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.

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by