行业知识
Matlab进阶绘图第46期—气泡分组柱状图
2026-12-03 13:18:43
来源:互联网转载
���泡分组柱状图是分组柱状图与气泡图的组合—在分组柱状图每组柱子上方添加大小不同的气泡,用于表示另外一个数据变量(如每组柱子值的和)的大小。
本文利用自己制作的BarBubble工具,进行气泡分组柱状图的绘制,先来看一下成品效果:
特别提示:本期内容『数据+代码』已上传资源群中,加群的朋友请自行下载。有需要的朋友可以关注同名公号【阿昆的科研日常】,后台回复关键词【绘图桶】查看加入方式。
1. 数据准备
此部分主要是读取原始数据并初始化绘图参数。
% 读取数据load data.mat% 初始化绘图参数bardata = y1;bubbledata = y2;offset = 80;bsz = [10 50];
2. 颜色定义
作图不配色就好比做菜不放盐,总让人感觉少些味道。
但颜色搭配比较考验个人审美,需要多加尝试。
这里直接使用TheColor配色工具中的SCI权威配色库:
%% 颜色定义C = TheColor('sci',2040,'map',4);C = flipud(C); 3. 气泡分组柱状图绘制
调用‘BarBubble’和命令,绘制初始气泡分组柱状图。
[b1,b2] = BarBubble(bardata,bubbledata,offset,bsz,'normal');hTitle = title('Grouped Bar with Bubble');hXLabel = xlabel('Samples');hYLabel = ylabel('Number'); 4. 细节优化
为了插图的美观与信息完整性,对图形细节等进行美化:
% 气泡分组柱状图属性调整% 分组柱状图for i = 1:4 b1(i).FaceColor = C(i,1:3); b1(i).BarWidth = 0.8; b1(i).LineWidth = 0.8;end% 气泡图b2.MarkerFaceColor = [0.2 0.2 0.2];b2.MarkerFaceAlpha = 1;b2.MarkerEdgeColor = [0.2 0.2 0.2];% 坐标区调整set(gca, 'Box', 'off', ... % 边框 'LineWidth', 1,... % 线宽 'XGrid', 'on', 'YGrid', 'on', ... % 网格 'TickDir', 'out', 'TickLength', [.015 .015], ... % 刻度 'XMinorTick', 'off', 'YMinorTick', 'off', ... % 小刻度 'XColor', [.1 .1 .1], 'YColor', [.1 .1 .1]) % 坐标轴颜色% 坐标轴刻度调整set(gca, 'YTick', 0:20:120,... 'Ylim' , [0 100], ... 'Xlim' , [0.5 4.5], ... 'XTick', 1:6,... 'Xticklabel',{'S1' 'S2' 'S3' 'S4'})% 气泡图图例blgd = bubblelegend('Location','southeastoutside',... 'Style','vertical',... 'BubbleSizeOrder','descending',... 'box','off',... 'NumBubbles',3,... ... 'FontName', 'Arial',... 'FontSize', 11);% 分组柱状图图例 hLegend = legend(b1, ... ' A1', ' A2', ' A3', ' A4', ... 'Position',[0.75,0.77,0.11,0.15],... 'Orientation','vertical');legend('boxoff');% 字体和字号set(gca, 'FontName', 'Arial', 'FontSize', 10)set([hLegend, hXLabel, hYLabel], 'FontSize', 11, 'FontName', 'Arial')set(hTitle, 'FontSize', 12, 'FontWeight' , 'bold')% 背景颜色set(gcf,'Color',[1 1 1])% 添加上、右框线xc = get(gca,'XColor');yc = get(gca,'YColor');unit = get(gca,'units');ax = axes( 'Units', unit,... 'Position',get(gca,'Position'),... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none',... 'XColor',xc,... 'YColor',yc);set(ax, 'linewidth',1,... 'XTick', [],... 'YTick', []); 设置完毕后,以期刊所需分辨率、格式输出图片。
%% 图片输出figW = figureWidth;figH = figureHeight;set(figureHandle,'PaperUnits',figureUnits);set(figureHandle,'PaperPosition',[0 0 figW figH]);fileout = 'test';print(figureHandle,[fileout,'.png'],'-r300','-dpng');
下一篇:小程序口令红包怎么用?