matlab画双重柱状图,并上移另外一组

文摘   2024-10-05 17:42   德国  

这个图片很有意思:娱乐局

于是使用matlab重复以下:

本想使用matlab 的bar,但发现使用上次的基线平移会失败,因为会把所有的柱状图都放在同一个基线上面:

就是用自己的patch.和for循环结合下:

for i = 1:11    xx = [i-0.4 i+0.4 i+0.4 i-0.4];    y = [num-(i-1)*num/num2+x1(i) num-(i-1)*num/num2+x1(i) num-(i-1)*num/num2+x2(i)+x1(i) num-(i-1)*num/num2+x2(i)+x1(i)];    patch(xx,y,[3 166,44]./255,'edgecolor','none')    hold onend

全部代码如下:


clear;clc;close all;%%数据构造:x1 = 11:-1:1;x2 = 22:-1:12;x3= 33:-1:23;x = cat(1,x1,x2,x3);figureset(gcf,'position',[50 50 850 500],'color','w')h=bar(1:11,ones(11,1)*100,0.8);set(h(1),'FaceColor',[135 206 250]./255);hold ony = 1:11;h=bar(y,x,'stacked');set(h(1),'FaceColor',[242,159,5]./255);set(h(2),'FaceColor',[3 166,44]./255);set(h(3),'FaceColor',[242 5 5]./255);
for i = 1:10 hold on line([0.6 12-0.6],[i*10 i*10],'color','k')endxlim([0.6 12-0.6])set(gca,'xtick',[1:11],'xticklabel',[1:11],'ytick',[0:10:100],'yticklabel',[0:10:100],'linewidth',1)ytickformat('%,.0f \%')export_fig('bar_1.jpg','-r600')%%clear;clc;close all;%%数据构造:x1 = 11:-1:1;x2 = 22:-1:12;x3= 33:-1:23;x = cat(1,x1,x2,x3);num = 20;% 平移距离num2 = 15;% 控制下移距离gray = [0.6 0.6 0.6];figureset(gcf,'position',[50 50 850 500],'color','w')h=bar(1:11,ones(11,1)*100,0.8);set(h(1),'FaceColor',[135 206 250]./255);hold on%01for i = 1:11 xx = [i-0.4 i+0.4 i+0.4 i-0.4]; y = [num-(i-1)*num/num2 num-(i-1)*num/num2 num-(i-1)*num/num2+x1(i) num-(i-1)*num/num2+x1(i)]; patch(xx,y,[242,159,5]./255,'edgecolor','none') hold onend% 02for i = 1:11 xx = [i-0.4 i+0.4 i+0.4 i-0.4]; y = [num-(i-1)*num/num2+x1(i) num-(i-1)*num/num2+x1(i) num-(i-1)*num/num2+x2(i)+x1(i) num-(i-1)*num/num2+x2(i)+x1(i)]; patch(xx,y,[3 166,44]./255,'edgecolor','none') hold onend% 03for i = 1:11 xx = [i-0.4 i+0.4 i+0.4 i-0.4]; y = [num-(i-1)*num/num2+x1(i)+x2(i) num-(i-1)*num/num2+x1(i)+x2(i) num-(i-1)*num/num2+x2(i)+x1(i)+x2(i) num-(i-1)*num/num2+x2(i)+x1(i)+x2(i)]; patch(xx,y,[242 5 5]./255,'edgecolor','none') hold onendylim([0 100])xlim([0.6 12-0.6])% 竖线box onfor i = 1:11 hold on line([i-0.4 i-0.4],[0 100],'color',gray) hold on line([i+0.4 i+0.4],[0 100],'color',gray)end% % 横线for i = 1:10 hold on line([0.6 12-0.6],[i*10 i*10],'color',gray)endset(gca,'xtick',[1:11],'xticklabel',[1:11],'ytick',[0:10:100],'yticklabel',[0:10:100],'linewidth',1)ytickformat('%,.0f \%')export_fig('bar_2.jpg','-r600')


海洋与大气科学
海洋与大气科学数据分析,数据可视化分享,可教学。
 最新文章