bar graph with broken y-axis
조회 수: 32 (최근 30일)
이전 댓글 표시
Does anyone have code for creating a vertical bar graph with a broken y-axis?
댓글 수: 0
답변 (5개)
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!