Matlab绘制多组柱状图的方法(可直接复制)

前言

论文中常有需要用柱状图表示数据的情况,本文给出Matlab绘制多组数据的柱状图的代码,并给出常用论文示图配色。显示效果如下:在这里插入图片描述

脚本代码

%% 数据准备
BMRKSH=[67.98 43.19 65.72 30.97 26.90 33.78 56.76 66.03 67.56];
TCA=[63.82 33.26 62.53 34.34 24.34 31.91 50.17 59.13 67.08];
IGLDA=[64.69 34.69 63.33 34.31 23.78 31.74 50.56 59.83 67.12];
TIT=[62.26 34.03 65.94 29.20 24.27 31.98 46.88 55.52 68.75];
dataset = [BMRKSH;TCA;IGLDA;TIT]';
%% 颜色定义
% addcolor函数中270种颜色对照表(见文章底部)
% https://blog.csdn.net/qq_37233260/article/details/118642983
C1 = addcolor(185); 
C2 = 'k';
C3 = addcolor(178);
C4 = addcolor(130);% 紫色

%% 图片尺寸设置(单位:厘米)
figureUnits = 'centimeters';
figureWidth = 20;
figureHeight = 15;

%% 柱状图绘制
%窗口设置
figureHandle = figure;
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]); % define the new figure dimensions
hold on

% 绘制柱图
% 1-调节柱间距
GO = bar(dataset,1,'EdgeColor','k');

% 赋色
GO(1).FaceColor = C1;
GO(2).FaceColor = C2;
GO(3).FaceColor = C3;
GO(4).FaceColor = C4;

% 文字注释,不需要可删
% for ii=1:5
%     text(ii-0.24,dataset(ii,1)+0.005,num2str(dataset(ii,1)),...
%          'ROtation',90,'color',C1,'FontSize',10,'FontName',  'Helvetica');
%     text(ii,dataset(ii,2)+0.01,num2str(dataset(ii,2)),...
%          'ROtation',90,'color',C2,'FontSize',10,'FontName',  'Helvetica');     
%     text(ii+0.22,dataset(ii,3)+0.01,num2str(dataset(ii,3)),...
%          'ROtation',90,'color',C3,'FontSize',10,'FontName',  'Helvetica');  
% end

% 坐标区调整
set(gca, 'Box', 'off', ...                                         % 边框
         'XGrid', 'off', 'YGrid', 'on', ...                        % 网格
         'TickDir', 'out', 'TickLength', [.02 .02], ...            % 刻度
         'XMinorTick', 'off', 'YMinorTick', 'off', ...             % 小刻度
         'XColor', [.1 .1 .1],  'YColor', [.1 .1 .1],...           % 坐标轴颜色
         'YTick', 0:10:100,...                                      % 刻度位置、间隔
         'Ylim' , [0 100], ...                                     % 坐标轴范围
         'Xticklabel',{'A01' 'A02' 'A03' 'A04' 'A05' 'A06' 'A07' 'A08' 'A09'},...% X坐标轴刻度标签
         'Yticklabel',{[0:10:100]})                                 % Y坐标轴刻度标签

% 标签及Legend 设置    
hYLabel = ylabel('Accuracy (%)');
hXLabel = xlabel('Test Subject');
hLegend = legend([GO(1),GO(2),GO(3),GO(4)], ...
    'BMRKSH', 'TCA', 'IGLDA','TIT', ...
                 'Location', 'northeast');
% Legend位置微调 
P = hLegend.Position;
hLegend.Position = P + [0.015 0.03 0 0];

% 刻度标签字体和字号
set(gca, 'FontName', 'Times', 'FontSize', 9)
% 标签及Legend的字体字号 
set([hYLabel,hXLabel,hLegend], 'FontName',  'Helvetica')
set([hYLabel,hXLabel,hLegend], 'FontSize', 10)

% 背景颜色
set(gca,'Color',[1 1 1])

%% 图片输出
figW = figureWidth;
figH = figureHeight;
set(figureHandle,'PaperUnits',figureUnits);
set(figureHandle,'PaperPosition',[0 0 figW figH]);
fileout = 'eg';
print(figureHandle,[fileout,'.png'],'-r300','-dpng');

addcolor函数

function map = addcolor(N)
% N为颜色种类

C = [0	0	0
0.549019607843137	0.549019607843137	0.549019607843137
0.627450980392157	0.627450980392157	0.627450980392157
0.756862745098039	0.756862745098039	0.756862745098039
0.827450980392157	0.827450980392157	0.827450980392157
0.882352941176471	0.882352941176471	0.882352941176471
0.905882352941177	0.905882352941177	0.905882352941177
0.972549019607843	0.972549019607843	0.972549019607843
1	0.980392156862745	0.980392156862745
0.737254901960784	0.560784313725490	0.560784313725490
0.941176470588235	0.501960784313726	0.501960784313726
0.803921568627451	0.360784313725490	0.360784313725490
0.647058823529412	0.164705882352941	0.164705882352941
0.698039215686275	0.133333333333333	0.133333333333333
0.501960784313726	0	0
0.545098039215686	0	0
1	0	0
1	0.894117647058824	0.882352941176471
0.980392156862745	0.501960784313726	0.447058823529412
1	0.388235294117647	0.278431372549020
0.913725490196078	0.588235294117647	0.478431372549020
1	0.498039215686275	0.313725490196078
1	0.270588235294118	0
1	0.627450980392157	0.478431372549020
0.627450980392157	0.321568627450980	0.176470588235294
1	0.960784313725490	0.933333333333333
0.823529411764706	0.411764705882353	0.117647058823529
0.545098039215686	0.270588235294118	0.0745098039215686
0.956862745098039	0.643137254901961	0.376470588235294
1	0.854901960784314	0.725490196078431
0.803921568627451	0.521568627450980	0.247058823529412
0.980392156862745	0.941176470588235	0.901960784313726
1	0.894117647058824	0.768627450980392
1	0.549019607843137	0
0.870588235294118	0.721568627450980	0.529411764705882
0.980392156862745	0.921568627450980	0.843137254901961
0.823529411764706	0.705882352941177	0.549019607843137
1	0.870588235294118	0.678431372549020
1	0.921568627450980	0.803921568627451
1	0.937254901960784	0.835294117647059
1	0.894117647058824	0.709803921568628
1	0.647058823529412	0
0.960784313725490	0.870588235294118	0.701960784313725
0.992156862745098	0.960784313725490	0.901960784313726
1	0.980392156862745	0.941176470588235
0.721568627450980	0.525490196078431	0.0431372549019608
0.854901960784314	0.647058823529412	0.125490196078431
1	0.972549019607843	0.862745098039216
1	0.843137254901961	0
1	0.980392156862745	0.803921568627451
0.941176470588235	0.901960784313726	0.549019607843137
0.933333333333333	0.909803921568627	0.666666666666667
0.741176470588235	0.717647058823529	0.419607843137255
1	1	0.941176470588235
0.960784313725490	0.960784313725490	0.862745098039216
1	1	0.878431372549020
0.980392156862745	0.980392156862745	0.823529411764706
0.501960784313726	0.501960784313726	0
0.749019607843137	0.749019607843137	0
1	1	0
0.419607843137255	0.556862745098039	0.137254901960784
0.603921568627451	0.803921568627451	0.196078431372549
0.333333333333333	0.419607843137255	0.184313725490196
0.678431372549020	1	0.184313725490196
0.498039215686275	1	0
0.486274509803922	0.988235294117647	0
0.941176470588235	1	0.941176470588235
0.560784313725490	0.737254901960784	0.560784313725490
0.596078431372549	0.984313725490196	0.596078431372549
0.564705882352941	0.933333333333333	0.564705882352941
0.133333333333333	0.545098039215686	0.133333333333333
0.196078431372549	0.803921568627451	0.196078431372549
0	0.392156862745098	0
0	0.501960784313726	0
0	1	0
0.180392156862745	0.545098039215686	0.341176470588235
0.235294117647059	0.701960784313725	0.443137254901961
0	1	0.498039215686275
0.960784313725490	1	0.980392156862745
0	0.980392156862745	0.603921568627451
0.400000000000000	0.803921568627451	0.666666666666667
0.498039215686275	1	0.831372549019608
0.250980392156863	0.878431372549020	0.815686274509804
0.125490196078431	0.698039215686275	0.666666666666667
0.282352941176471	0.819607843137255	0.800000000000000
0.941176470588235	1	1
0.878431372549020	1	1
0.686274509803922	0.933333333333333	0.933333333333333
0.184313725490196	0.309803921568627	0.309803921568627
0	0.501960784313726	0.501960784313726
0	0.545098039215686	0.545098039215686
0	0.749019607843137	0.749019607843137
0	1	1
0	0.807843137254902	0.819607843137255
0.372549019607843	0.619607843137255	0.627450980392157
0.690196078431373	0.878431372549020	0.901960784313726
0.678431372549020	0.847058823529412	0.901960784313726
0	0.749019607843137	1
0.529411764705882	0.807843137254902	0.921568627450980
0.529411764705882	0.807843137254902	0.980392156862745
0.274509803921569	0.509803921568627	0.705882352941177
0.941176470588235	0.972549019607843	1
0.117647058823529	0.564705882352941	1
0.466666666666667	0.533333333333333	0.600000000000000
0.439215686274510	0.501960784313726	0.564705882352941
0.690196078431373	0.768627450980392	0.870588235294118
0.392156862745098	0.584313725490196	0.929411764705882
0.254901960784314	0.411764705882353	0.882352941176471
0.972549019607843	0.972549019607843	1
0.901960784313726	0.901960784313726	0.980392156862745
0.0980392156862745	0.0980392156862745	0.439215686274510
0	0	0.501960784313726
0	0	0.545098039215686
0	0	0.803921568627451
0	0	1
0.415686274509804	0.352941176470588	0.803921568627451
0.282352941176471	0.239215686274510	0.545098039215686
0.482352941176471	0.407843137254902	0.933333333333333
0.576470588235294	0.439215686274510	0.858823529411765
0.400000000000000	0.200000000000000	0.600000000000000
0.541176470588235	0.168627450980392	0.886274509803922
0.294117647058824	0	0.509803921568627
0.600000000000000	0.196078431372549	0.800000000000000
0.580392156862745	0	0.827450980392157
0.729411764705882	0.333333333333333	0.827450980392157
0.847058823529412	0.749019607843137	0.847058823529412
0.866666666666667	0.627450980392157	0.866666666666667
0.933333333333333	0.509803921568627	0.933333333333333
0.501960784313726	0	0.501960784313726
0.545098039215686	0	0.545098039215686
0.749019607843137	0	0.749019607843137
1	0	1
0.854901960784314	0.439215686274510	0.839215686274510
0.780392156862745	0.0823529411764706	0.521568627450980
1	0.0784313725490196	0.576470588235294
1	0.411764705882353	0.705882352941177
1	0.941176470588235	0.960784313725490
0.858823529411765	0.439215686274510	0.576470588235294
0.862745098039216	0.0784313725490196	0.235294117647059
1	0.752941176470588	0.796078431372549
1	0.713725490196078	0.756862745098039
0.984313725490196	0.705882352941177	0.682352941176471
0.701960784313725	0.803921568627451	0.890196078431373
0.800000000000000	0.921568627450980	0.772549019607843
0.870588235294118	0.796078431372549	0.894117647058824
0.996078431372549	0.850980392156863	0.650980392156863
1	1	0.800000000000000
0.898039215686275	0.847058823529412	0.741176470588235
0.992156862745098	0.854901960784314	0.925490196078431
0.949019607843137	0.949019607843137	0.949019607843137
0.800000000000000	0.800000000000000	0.800000000000000
0.945098039215686	0.886274509803922	0.800000000000000
1	0.949019607843137	0.682352941176471
0.901960784313726	0.960784313725490	0.788235294117647
0.956862745098039	0.792156862745098	0.894117647058824
0.796078431372549	0.835294117647059	0.909803921568627
0.992156862745098	0.803921568627451	0.674509803921569
0.701960784313725	0.886274509803922	0.803921568627451
0.650980392156863	0.807843137254902	0.890196078431373
0.121568627450980	0.470588235294118	0.705882352941177
0.698039215686275	0.874509803921569	0.541176470588235
0.200000000000000	0.627450980392157	0.172549019607843
0.984313725490196	0.603921568627451	0.600000000000000
0.890196078431373	0.101960784313725	0.109803921568627
0.992156862745098	0.749019607843137	0.435294117647059
1	0.498039215686275	0
0.792156862745098	0.698039215686275	0.839215686274510
0.415686274509804	0.239215686274510	0.603921568627451
1	1	0.600000000000000
0.694117647058824	0.349019607843137	0.156862745098039
0.498039215686275	0.788235294117647	0.498039215686275
0.745098039215686	0.682352941176471	0.831372549019608
0.992156862745098	0.752941176470588	0.525490196078431
0.219607843137255	0.423529411764706	0.690196078431373
0.941176470588235	0.00784313725490196	0.498039215686275
0.749019607843137	0.356862745098039	0.0862745098039216
0.400000000000000	0.400000000000000	0.400000000000000
0.105882352941176	0.619607843137255	0.466666666666667
0.850980392156863	0.372549019607843	0.00784313725490196
0.458823529411765	0.439215686274510	0.701960784313725
0.905882352941177	0.160784313725490	0.541176470588235
0.400000000000000	0.650980392156863	0.117647058823529
0.901960784313726	0.670588235294118	0.00784313725490196
0.650980392156863	0.462745098039216	0.113725490196078
0.894117647058824	0.101960784313725	0.109803921568627
0.215686274509804	0.494117647058824	0.721568627450980
0.301960784313725	0.686274509803922	0.290196078431373
0.596078431372549	0.305882352941177	0.639215686274510
1	1	0.200000000000000
0.650980392156863	0.337254901960784	0.156862745098039
0.968627450980392	0.505882352941176	0.749019607843137
0.600000000000000	0.600000000000000	0.600000000000000
0.400000000000000	0.760784313725490	0.647058823529412
0.988235294117647	0.552941176470588	0.384313725490196
0.552941176470588	0.627450980392157	0.796078431372549
0.905882352941177	0.541176470588235	0.764705882352941
0.650980392156863	0.847058823529412	0.329411764705882
1	0.850980392156863	0.184313725490196
0.898039215686275	0.768627450980392	0.580392156862745
0.701960784313725	0.701960784313725	0.701960784313725
0.552941176470588	0.827450980392157	0.780392156862745
1	1	0.701960784313725
0.745098039215686	0.729411764705882	0.854901960784314
0.984313725490196	0.501960784313726	0.447058823529412
0.501960784313726	0.694117647058824	0.827450980392157
0.992156862745098	0.705882352941177	0.384313725490196
0.701960784313725	0.870588235294118	0.411764705882353
0.988235294117647	0.803921568627451	0.898039215686275
0.850980392156863	0.850980392156863	0.850980392156863
0.737254901960784	0.501960784313726	0.741176470588235
1	0.929411764705882	0.435294117647059
0.121568627450980	0.466666666666667	0.705882352941177
1	0.498039215686275	0.0549019607843137
0.172549019607843	0.627450980392157	0.172549019607843
0.839215686274510	0.152941176470588	0.156862745098039
0.580392156862745	0.403921568627451	0.741176470588235
0.549019607843137	0.337254901960784	0.294117647058824
0.890196078431373	0.466666666666667	0.760784313725490
0.498039215686275	0.498039215686275	0.498039215686275
0.737254901960784	0.741176470588235	0.133333333333333
0.0901960784313726	0.745098039215686	0.811764705882353
0.682352941176471	0.780392156862745	0.909803921568627
1	0.733333333333333	0.470588235294118
0.596078431372549	0.874509803921569	0.541176470588235
1	0.596078431372549	0.588235294117647
0.772549019607843	0.690196078431373	0.835294117647059
0.768627450980392	0.611764705882353	0.580392156862745
0.968627450980392	0.713725490196078	0.823529411764706
0.780392156862745	0.780392156862745	0.780392156862745
0.858823529411765	0.858823529411765	0.552941176470588
0.619607843137255	0.854901960784314	0.898039215686275
0.223529411764706	0.231372549019608	0.474509803921569
0.321568627450980	0.329411764705882	0.639215686274510
0.419607843137255	0.431372549019608	0.811764705882353
0.611764705882353	0.619607843137255	0.870588235294118
0.388235294117647	0.474509803921569	0.223529411764706
0.549019607843137	0.635294117647059	0.321568627450980
0.709803921568628	0.811764705882353	0.419607843137255
0.807843137254902	0.858823529411765	0.611764705882353
0.549019607843137	0.427450980392157	0.192156862745098
0.741176470588235	0.619607843137255	0.223529411764706
0.905882352941177	0.729411764705882	0.321568627450980
0.905882352941177	0.796078431372549	0.580392156862745
0.517647058823530	0.235294117647059	0.223529411764706
0.678431372549020	0.286274509803922	0.290196078431373
0.839215686274510	0.380392156862745	0.419607843137255
0.905882352941177	0.588235294117647	0.611764705882353
0.482352941176471	0.254901960784314	0.450980392156863
0.647058823529412	0.317647058823529	0.580392156862745
0.807843137254902	0.427450980392157	0.741176470588235
0.870588235294118	0.619607843137255	0.839215686274510
0.192156862745098	0.509803921568627	0.741176470588235
0.419607843137255	0.682352941176471	0.839215686274510
0.619607843137255	0.792156862745098	0.882352941176471
0.776470588235294	0.858823529411765	0.937254901960784
0.901960784313726	0.333333333333333	0.0509803921568627
0.992156862745098	0.552941176470588	0.235294117647059
0.992156862745098	0.682352941176471	0.419607843137255
0.992156862745098	0.815686274509804	0.635294117647059
0.192156862745098	0.639215686274510	0.329411764705882
0.454901960784314	0.768627450980392	0.462745098039216
0.631372549019608	0.850980392156863	0.607843137254902
0.780392156862745	0.913725490196078	0.752941176470588
0.458823529411765	0.419607843137255	0.694117647058824
0.619607843137255	0.603921568627451	0.784313725490196
0.737254901960784	0.741176470588235	0.862745098039216
0.854901960784314	0.854901960784314	0.921568627450980
0.388235294117647	0.388235294117647	0.388235294117647
0.588235294117647	0.588235294117647	0.588235294117647
0.741176470588235	0.741176470588235	0.741176470588235];

map = C(N,:);
end

色彩对照表

在这里插入图片描述
文章代码是我修改后的,原始代码出自vx公众号“阿昆的科研日常”,在此感谢作者。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值