1 分組繪制柱狀圖
1.1 案例1:常規(guī)分組柱狀圖
繪制分組數(shù)據(jù),并對(duì)數(shù)據(jù)格式和邊框等進(jìn)行設(shè)置,成圖如下所示:
MATLAB繪制代碼如下:
clc
close all
clear
%% 導(dǎo)入數(shù)據(jù)
pathFigure= '.\Figures\' ;
Name = ["生活","生態(tài)","工業(yè)","農(nóng)業(yè)","匯總"];
xLables = ['Ⅰ' ; 'Ⅱ'; 'Ⅲ'];
mycolor = [53,42,134;
21,132,212;
55,184,156;
215,186,84;
247,250,13]./255;
x = [1 2 3];
XX = [0 0 0 3.92 2.78
0 0 0 6.63 4.2
0.78 8.37 9.98 22.15 15.46];
figure(1);
hold on;
box on;
h = bar( XX ,'FaceColor','flat');
for k = 1:size(XX,2)
h(k).FaceColor = mycolor(k,:);
end
set(gca,'XTick', x , 'XTickLabel',xLables);
set(gca,'FontSize',12,'Fontname', 'Times New Roman');
xlabel("情景",'FontName','宋體','FontSize',14,'Fontweight','bold'); % 后續(xù)調(diào)整坐標(biāo)標(biāo)題
ylabel("\fontname{宋體}\fontsize{15}缺水率(\fontname{Times New Roman}\fontsize{15}%\fontname{宋體}\fontsize{15})",'FontSize',14,'Fontweight','bold'); % 后續(xù)調(diào)整坐標(biāo)標(biāo)題
hl = legend( Name );
set(hl,'Box','off','location','NorthWest','NumColumns',1,'FontSize',12,'FontName','宋體');
ax1 = gca;
ax1.FontSize = 12;
ax1.LineWidth = 1;
set(gca,'Layer','top');
str= strcat(pathFigure, "Figure1", '.tiff');
print(gcf, '-dtiff', '-r600', str);
2 繪制多組柱狀圖并修改文字顏色一致
圖源:J2022-Observed decrease in light precipitation in part due to urbanization-Scientific Reports
2.1 案例
3 繪制雙軸-倒立柱狀圖
成圖如下:
MATLAB代碼如下:
clc
close all
clear
%% 導(dǎo)入數(shù)據(jù)
pathFigure= '.\Figures\' ;
X = randn(40,1);
Ymax = 50;
Y = Ymax*randn(40,1);
%% 開(kāi)始繪圖
figure(1)
hold on;box on;grid off;
h(3) = area( X,'FaceColor', [0.5529,0.7137,0.8039] ,'LineStyle','none');
[AX,h(1),h(2)] = plotyy( 1:length(X), X, 1:length(X) , Y, 'plot','bar'); % 畫(huà)雙軸,AX(1)左軸,AX(2)右軸,H為曲線(xiàn)本身
set(AX(2),'YDir','reverse','Ylim',[0, Ymax*3 ],'YTick',[0:20:Ymax*2],'FontSize',12,'Fontname', 'Times New Roman'); % 設(shè)置右邊軸為倒立
set(gca,'box','off','Ytick',[])
set(AX(1),'YLim',[min(X)*1.5,1.5*max(X)],'YTick',[floor(min(X)*1.5):1:ceil(1.5*max(X)) ],'Fontsize',10,'YColor','k');
%設(shè)置坐標(biāo)軸的標(biāo)題
h(5) = plot( zeros( length(X) ,1) ,'k-','linewidth',0.75);
set(get(AX(1),'Xlabel'),'String','XLabel','Fontname', 'Times New Roman');
set(get(AX(1),'Ylabel'),'String','YLabel1','Fontname', 'Times New Roman');
set(get(AX(2),'Ylabel'),'string','YLabel2','LineStyle','none','Fontname', 'Times New Roman');
set(gca,'Layer','top','FontSize',12,'Fontname', 'Times New Roman');
str= strcat(pathFigure, "Figure 1", '.jpg');
print(gcf, '-djpeg', '-r600', str);
4 繪制區(qū)間位置柱狀圖
區(qū)間范圍柱狀圖是柱狀圖的一種,能夠更加清晰地展示各類(lèi)目數(shù)據(jù)區(qū)間范圍分布的差異。支持通過(guò)獲取數(shù)據(jù)的最小值和最大值之間的范圍,展示不同類(lèi)目下柱圖的數(shù)據(jù)樣式。 如下圖所示:
圖源:J2022-Amplification of non-stationary drought to heatwave duration and intensity in eastern China Spatiotemporal pattern and causes文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-492164.html
5 堆積柱狀圖
成果圖如下:
相關(guān)MATLAB繪圖代碼如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-492164.html
%% 圖形繪制
figure(1);
hold on;
box on;
h = bar( Data'*100,'stacked' ,'FaceColor','flat');
set(gca,'Layer','top','FontSize',12,'Fontname', 'Times New Roman');
set(gca,'YTick', [0:20:105] , 'YTickLabel', [0:20:100] ,'Fontname', 'Times New Roman');
set(gca,'XTick', [1.5:2:6] , 'XTickLabel', BasinName);
xlabel("流域",'FontName','宋體','FontSize',14,'Fontweight','bold'); % 后續(xù)調(diào)整坐標(biāo)標(biāo)題
ylabel("\fontname{宋體}貢獻(xiàn)率\fontname{Times New Roman}/%",'FontSize',14,'Fontweight','bold'); % 后續(xù)調(diào)整坐標(biāo)標(biāo)題
hl = legend( Name );
set(hl,'Box','off','location','NorthOutside','NumColumns',6,'FontSize',10,'FontName','Times New Roman');
set(gca,'Layer','top');
str= strcat(pathFigure, "Figure1", '.tiff');
print(gcf, '-dtiff', '-r600', str);
參考
到了這里,關(guān)于【MATLAB基礎(chǔ)繪圖第7棒】繪制各式柱狀圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!