Matlab内置的柱状图函数bar使用方法

标题:Matlab内置的柱状图函数bar使用方法

       在2019年9月发布的《在Matlab中使用barweb绘制带方差的分组柱状图时的几个注意事项》中介绍了由第三方编写的barweb函数使用方法,但有时其实并不需要画带方差的分组柱状图,很多时候只需要使用Matlab内置的柱状图函数bar即可。本篇简单介绍一下bar函数的使用方法,其实主要是介绍一下如何设置横/纵坐标以及柱状图的颜色图等。

       首先,看以下关于bar的demo使用程序:

%demo_bar.m@2021-11-21
close all;clear;clc;
X = 5+rand(6,3);%随机生成6*3的数据矩阵
 
legends = {'Type-I','Type-II','Type-III'};
groupnames = {'Group1','Group2','Group3','Group4','Group5','Group6'};
Title = 'title of bar example';
Xlabel = 'xlabel of bar example ($\omega$)';
Ylabel = 'ylabel of bar exampe ($\Omega$)';
figure;bar(X);colormap(gray);
legend(legends,'fontsize',16,'location','northeast');
xlabel(Xlabel, 'Interpreter','latex', 'FontName', 'Times New Roman', 'FontSize',16);
ylabel(Ylabel, 'Interpreter','latex', 'FontName', 'Times New Roman', 'FontSize',16);
set(gca, 'xticklabel', groupnames, 'box', 'off', 'fontsize', 16, 'xtick',1:length(groupnames));
xlim([0.5,length(groupnames)+0.5]);
%print('bar_demo','-depsc2','-r600');

运行之后可以得到如下图形:

 注意:(1) legends中的图例个数对应数据X的列个数,groupnames中的组名称个数对应数据X的行个数;(2) 在Xlabel和Ylabel字符串中的括号内$$之间的内容是LaTeX符号;(3) 第10行代码就是调用bar函数绘制柱状图;(4)第10行的colormap(gray)用来设置柱状图的颜色图,接下来会介绍更多使用细节;(5) 第11行使用legend设置图例格式,其中字号为16号,位置为右上角,可以通过命令“help legend”查看legend函数的更多使用细节;(6)第12行和第13行分别使用xlabel和ylabel设置横/纵坐标轴的名称,其中解释器使用LaTeX,字体设置为Times New Roman,字号为16号;(7) 第14行使用set设置横坐标刻度,分别在1到6的位置 (即'xtick',1:length(groupnames))显示Group1到Group6 (即'xticklabel', groupnames),字号为16号;(8) 第15行使用xlim函数将横坐标显示范围设置在0.5到6.5之间。

       接下来再介绍一下如何根据自己的需要设置柱状图的颜色图,该功能由colormap函数实现,通过在Matlab的命令行窗口输入“help colormap”可以查看该函数的帮助信息(不同Matlab显示会有所不同,以下是R2014a的信息):

        点击图中的“colormap 的参考页”可以查看更详细的说明文件,或者直接打开以下链接:

       中文版:查看并设置当前颜色图 - MATLAB colormap- MathWorks 中国

       英文版:View and set current colormap - MATLAB colormap- MathWorks China

       以下是中文版链接中介绍的Matlab内置的颜色图:

 以下是R2014a的“colormap 的参考页”中关于各颜色图的介绍:

  • autumn varies smoothly from red, through orange, to yellow.
  • bone is a grayscale colormap with a higher value for the blue component. This colormap is useful for adding an "electronic" look to grayscale images.
  • colorcube contains as many regularly spaced colors in RGB color space as possible, while attempting to provide more steps of gray, pure red, pure green, and pure blue.
  • cool consists of colors that are shades of cyan and magenta. It varies smoothly from cyan to magenta.
  • copper varies smoothly from black to bright copper.
  • flag consists of the colors red, white, blue, and black. This colormap completely changes color with each index increment.
  • gray returns a linear grayscale colormap.
  • hot varies smoothly from black through shades of red, orange, and yellow, to white.
  • hsv varies the hue component of the hue-saturation-value color model. The colors begin with red, pass through yellow, green, cyan, blue, magenta, and return to red. The colormap is particularly appropriate for displaying periodic functions. hsv(m) is the same as hsv2rgb([h ones(m,2)]) where h is the linear ramp, h = (0:m–1)'/m.
  • jet ranges from blue to red, and passes through the colors cyan, yellow, and orange. It is a variation of the hsv colormap. The jet colormap is associated with an astrophysical fluid jet simulation from the National Center for Supercomputer Applications. See Examples.
  • lines produces a colormap of colors specified by the axes ColorOrder property and a shade of gray.
  • pink contains pastel shades of pink. The pink colormap provides sepia tone colorization of grayscale photographs.
  • prism repeats the six colors red, orange, yellow, green, blue, and violet.
  • spring consists of colors that are shades of magenta and yellow.
  • summer consists of colors that are shades of green and yellow.
  • white is an all white monochrome colormap.
  • winter consists of colors that are shades of blue and green.

       实际上,每种颜色图名称对应一个函数,你也可以在Matlab中help每种颜色图名称查看相应的帮助信息,例如:

每种颜色图对应一个64*3的矩阵,每一行对应一种RGB颜色组合;可以在Matlab的命令行窗口中直接输入颜色图名称将该矩阵输出看一看(颜色图函数可以不包含输入参数),例如对于gray,可以直接输出gray返回该矩阵值,以下是我备份的gray矩阵的值: 

Gray_mtx = [
        0         0         0
    0.0159    0.0159    0.0159
    0.0317    0.0317    0.0317
    0.0476    0.0476    0.0476
    0.0635    0.0635    0.0635
    0.0794    0.0794    0.0794
    0.0952    0.0952    0.0952
    0.1111    0.1111    0.1111
    0.1270    0.1270    0.1270
    0.1429    0.1429    0.1429
    0.1587    0.1587    0.1587
    0.1746    0.1746    0.1746
    0.1905    0.1905    0.1905
    0.2063    0.2063    0.2063
    0.2222    0.2222    0.2222
    0.2381    0.2381    0.2381
    0.2540    0.2540    0.2540
    0.2698    0.2698    0.2698
    0.2857    0.2857    0.2857
    0.3016    0.3016    0.3016
    0.3175    0.3175    0.3175
    0.3333    0.3333    0.3333
    0.3492    0.3492    0.3492
    0.3651    0.3651    0.3651
    0.3810    0.3810    0.3810
    0.3968    0.3968    0.3968
    0.4127    0.4127    0.4127
    0.4286    0.4286    0.4286
    0.4444    0.4444    0.4444
    0.4603    0.4603    0.4603
    0.4762    0.4762    0.4762
    0.4921    0.4921    0.4921
    0.5079    0.5079    0.5079
    0.5238    0.5238    0.5238
    0.5397    0.5397    0.5397
    0.5556    0.5556    0.5556
    0.5714    0.5714    0.5714
    0.5873    0.5873    0.5873
    0.6032    0.6032    0.6032
    0.6190    0.6190    0.6190
    0.6349    0.6349    0.6349
    0.6508    0.6508    0.6508
    0.6667    0.6667    0.6667
    0.6825    0.6825    0.6825
    0.6984    0.6984    0.6984
    0.7143    0.7143    0.7143
    0.7302    0.7302    0.7302
    0.7460    0.7460    0.7460
    0.7619    0.7619    0.7619
    0.7778    0.7778    0.7778
    0.7937    0.7937    0.7937
    0.8095    0.8095    0.8095
    0.8254    0.8254    0.8254
    0.8413    0.8413    0.8413
    0.8571    0.8571    0.8571
    0.8730    0.8730    0.8730
    0.8889    0.8889    0.8889
    0.9048    0.9048    0.9048
    0.9206    0.9206    0.9206
    0.9365    0.9365    0.9365
    0.9524    0.9524    0.9524
    0.9683    0.9683    0.9683
    0.9841    0.9841    0.9841
    1.0000    1.0000    1.0000];

       如果使用默认的gray颜色图,则颜色是从黑(0 0 0)到白(1 1 1)。我在使用时很多时候想使用gray颜色图,但想把颜色顺序反过来使用,即从白到黑,那么可以对开头的程序做如下修改:

%demo_bar_gray.m@2021-11-21
close all;clear;clc;
X = 5+rand(6,3);%随机生成6*3的数据矩阵

gray_mtx_r = gray; gray_mtx_r=gray_mtx_r(64:-1:1,:);

legends = {'Type-I','Type-II','Type-III'};
groupnames = {'Group1','Group2','Group3','Group4','Group5','Group6'};
Title = 'title of bar example';
Xlabel = 'xlabel of bar example ($\omega$)';
Ylabel = 'ylabel of bar exampe ($\Omega$)';
figure;bar(X);colormap(gray_mtx_r);
legend(legends,'fontsize',16,'location','northeast');
xlabel(Xlabel, 'Interpreter','latex', 'FontName', 'Times New Roman', 'FontSize',16);
ylabel(Ylabel, 'Interpreter','latex', 'FontName', 'Times New Roman', 'FontSize',16);
set(gca, 'xticklabel', groupnames, 'box', 'off', 'fontsize', 16, 'xtick',1:length(groupnames));
xlim([0.5,length(groupnames)+0.5]);
%print('bar_demo','-depsc2','-r600');

运行之后可以得到如下图形:

注意:第5行不要使用直接使用“gray_mtx_r = gray(64:-1:1,:);”,这样会报错: 

这是因为gray本质上是一个函数,并不是一个矩阵。你可以在Matlab的命令行窗口中输入“edit gray”打开gray.m文件:

function g = gray(m)
%GRAY   Linear gray-scale color map
%   GRAY(M) returns an M-by-3 matrix containing a gray-scale colormap.
%   GRAY, by itself, is the same length as the current figure's
%   colormap. If no figure exists, MATLAB uses the length of the
%   default colormap.
%
%   For example, to reset the colormap of the current figure:
%
%             colormap(gray)
%
%   See also HSV, HOT, COOL, BONE, COPPER, PINK, FLAG, 
%   COLORMAP, RGBPLOT.

%   Copyright 1984-2015 The MathWorks, Inc.

if nargin < 1
   f = get(groot,'CurrentFigure');
   if isempty(f)
      m = size(get(groot,'DefaultFigureColormap'),1);
   else
      m = size(f.Colormap,1);
   end
end

g = (0:m-1)'/max(m-1,1);
g = [g g g];

       为了将gray颜色图的颜色反转过来,实际上已经使用了自定义的颜色图。其实也可以纯手工定义颜色图,只要设置RGB的值即可,例如:

%demo_bar_custom.m@2021-11-21
close all;clear;clc;
X = 5+rand(6,3);%随机生成6*3的数据矩阵

custom_map = [0 0 1; 0 1 0; 1 0 0];

legends = {'Type-I','Type-II','Type-III'};
groupnames = {'Group1','Group2','Group3','Group4','Group5','Group6'};
Title = 'title of bar example';
Xlabel = 'xlabel of bar example ($\omega$)';
Ylabel = 'ylabel of bar exampe ($\Omega$)';
figure;bar(X);colormap(custom_map);
legend(legends,'fontsize',16,'location','northeast');
xlabel(Xlabel, 'Interpreter','latex', 'FontName', 'Times New Roman', 'FontSize',16);
ylabel(Ylabel, 'Interpreter','latex', 'FontName', 'Times New Roman', 'FontSize',16);
set(gca, 'xticklabel', groupnames, 'box', 'off', 'fontsize', 16, 'xtick',1:length(groupnames));
xlim([0.5,length(groupnames)+0.5]);
%print('bar_demo','-depsc2','-r600');

运行之后可以得到如下图形:

 

这里每组只需要三种颜色,因此矩阵custom_map只包含三行,依次分别表示蓝(B)、绿(G)、红(R)。

       最后,说一个图片存储的技巧:平时可以使用“文件--->另存为”(save as),选择自己想要的格式保存即可,但如此存储的图片白色边框会比较大。还可以运行如下语句保存图片:

print('bar_demo','-depsc2','-r600');

 其中,bar_demo是文件名,-depsc2是文件格式,-r600是图片分辨率。可以help print,打开print函数的帮助文档查看,例如对-depsc2参数的解释(R2016b版本):

即存储为EPS彩色图片;而对分辨率参数-r600的解释: 

单独运行以上语句时,若有多张图片,最新单击浏览了哪张图片就会存储哪张图片,注意每次都要记得修改文件名,否则会覆盖前面的图片的。

        另外,在Matlab R2014a版本中,帮助文件中的内容无法正常复制,这是一个软件Bug,只需要使用快捷键 Ctrl+Insert 代替熟悉的复制快捷键 Ctrl+C即可。

 

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值