气泡堆叠蝴蝶图是堆叠蝴蝶 图与气泡图的组合---在堆叠蝴蝶 图每根柱子上方添加大小不同的气泡,用于表示另外一个数据变量(如每根柱子各组分的平均值)的大小。
本文利用自己制作的BarBubble 工具,进行气泡堆叠蝴蝶图的绘制,先来看一下成品效果:
特别提示:本期内容『数据+代码』已上传资源群中,加群的朋友请自行下载。有需要的朋友可以关注同名公号【阿昆的科研日常】,后台回复关键词【绘图桶】查看加入方式。
1. 数据准备
此部分主要是读取原始数据并初始化绘图参数。
% 读取数据load data.mat% 初始化绘图参数butterflydata1 = x1;butterflydata2 = x2;bubbledata1 = y1;bubbledata2 = y2;offset1 = 2700;textoffset1 = 300;offset2 = 2700;textoffset2 = 300;bsz = [5 30];Label={'Sample1','Sample2','Sample3','Sample4','Sample5','Sample6','Sample7','Sample8'};
2. 颜色定义
作图不配色就好比做菜不放盐,总让人感觉少些味道。
但颜色搭配比较考验个人审美,需要多加尝试。
这里直接使用TheColor配色工具中的SCI权威配色库:
C = TheColor('sci',2062,'map',10);C1 = C(1,1:3);C2 = C(2,1:3);C3 = C(3,1:3);C4 = C(4,1:3);C5 = C(10,1:3);C6 = C(9,1:3);C7 = C(8,1:3);C8 = C(7,1:3);
3. 气泡堆叠蝴蝶图绘制
调用'BarBubble'命令,绘制初始气泡堆叠蝴蝶图。
[ax1,ax2,b1,b2,bubble1,bubble2]=BubbleButterfly(figureHandle,butterflydata1,butterflydata2, ... bubbledata1,bubbledata2,offset1,offset2,bsz,Label,'stacked');% 添加左翼气泡注释for i = 1:length(bubbledata1) text(ax1, ... offset1+textoffset1, i, num2str(bubbledata1(i)), ... 'HorizontalAlignment','right', ... 'VerticalAlignment','middle', ... 'FontSize',10, ... 'FontName','Arial', ... 'color','k')end % 添加右翼气泡注释for i = 1:length(bubbledata2) text(ax2, ... offset2+textoffset2, i, num2str(bubbledata2(i)), ... 'HorizontalAlignment','left', ... 'VerticalAlignment','middle', ... 'FontSize',10, ... 'FontName','Arial', ... 'color','k')end
4. 细节优化
为了插图的美观与信息完整性,对图形细节等进行美化:
% 左翼优化% 蝴蝶图属性调整b1(1).FaceColor = C1;b1(2).FaceColor = C2;b1(3).FaceColor = C3;b1(4).FaceColor = C4;% 气泡属性调整bubble1.MarkerFaceColor = C1;bubble1.MarkerEdgeColor = C1;bubble1.MarkerFaceAlpha = 1;% 坐标区调整set(ax1, 'Box','off',... 'LineWidth',1,... 'TickLength',[0 0],... 'XGrid','off','YGrid','off',... 'XDir','reverse',... 'YDir','reverse',... 'YAxisLocation','right',... 'YTick',[])set(ax1, 'xtick',0:500:3000,... 'xlim', [0 3000],... 'ylim', [0.5 8.5])ax1.XRuler.Axle.LineStyle = 'none'; % 标签及Legend1设置hLegend1 = legend(ax1, ... 'F1','F2','F3','F4', ... 'Location', 'northoutside',... 'Orientation','horizontal');hLegend1.ItemTokenSize = [10 10];hLegend1.Box = 'off';% 字体字号set([ax1,hLegend1], 'FontName', 'Arial', 'FontSize', 9)% 右翼优化% 蝴蝶图属性调整b2(1).FaceColor = C5;b2(2).FaceColor = C6;b2(3).FaceColor = C7;b2(4).FaceColor = C8;% 气泡属性调整bubble2.MarkerFaceColor = C5;bubble2.MarkerEdgeColor = C5;bubble2.MarkerFaceAlpha = 1;set(ax2, 'Box','off',... 'LineWidth',1,... 'TickLength',[0 0],... 'XGrid','off','YGrid','off',... 'XDir','normal',... 'YDir','reverse',... 'YAxisLocation','left',... 'YTick',[])set(ax2, 'xtick',0:500:3000,... 'xlim', [0 3000],... 'ylim', [0.5 8.5])ax2.XRuler.Axle.LineStyle = 'none'; % 标签及Legend2设置hLegend2 = legend(ax2, ... 'F5','F6','F7','F8', ... 'Location', 'northoutside',... 'Orientation','horizontal');hLegend2.ItemTokenSize = [10 10];hLegend2.Box = 'off';% 字体字号set([ax2,hLegend2], 'FontName', 'Arial', 'FontSize', 9)% 背景颜色set(gcf,'Color',[1 1 1])
设置完毕后,以期刊所需分辨率、格式输出图片。
%% 图片输出figW = figureWidth;figH = figureHeight;set(figureHandle,'PaperUnits',figureUnits);set(figureHandle,'PaperPosition',[0 0 figW figH]);fileout = 'test';print(figureHandle,[fileout,'.png'],'-r300','-dpng');
以上。