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

news/2024/7/21 4:04:42 标签: matlab, 开发语言, 图像处理

标题: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即可。

 


http://www.niftyadmin.cn/n/715943.html

相关文章

在linux上操作mysql_linux 上操作mysql

1、 登录&#xff1a;mysql -u root -p 输入密码 设置用户密码 第一种办法: #mysqladmin -u root password "your password" show databases; show tables; 第二个办法: # mysql -uroot -pEnter password: 【输入原来的密码】mysql>use mysql;mysql&g…

photoshop CS2 笔记

启动时候按住ctrl shift alt,可以恢复photoshop原来的界面布局。 --------------------------------------------------------------------------------------------------------- 系列教程#05 01 Photoshop图层初识 我们使用形状工具中的椭圆〖U或SHIFT U〗来绘制&#xff0c;…

java多线程--同步屏障CyclicBarrier的使用

CyclicBarrier的概念理解: CyclicBarrier的字面上的意思是可循环的屏障,是java并发包java.util.concurrent 里的一个同步工具类,在我下载的JDK1.6的中文文档里对他的解释是: 大体意思就是:让一组线程到达一个屏障,一个集合点时,被阻塞,直到所有的线程都到了这个集合点时,屏障才…

Matlab中随机数生成控制

题目&#xff1a;Matlab中随机数生成控制 这里不讨论各随机数生成函数的使用方法&#xff08;例如rand, randn, randi, randperm等&#xff09;&#xff0c;重点讨论如何重复生成一样的随机数&#xff0c;因为很多算法中存在随机数生成环节&#xff0c;每次运行程序的结果都不一…

GitHub的DGit改进了平台的可靠性、性能以及可用性

GitHub最近悄悄地发布了DGit&#xff0c;全称为“分布式Git”。这是一种基于Git创建的分布式存储系统&#xff0c;其目标是改进使用GitHub时的可靠性、可用性以及性能。\\DGit是一个应用层面的协议&#xff0c;它利用了Git分布式的特性&#xff0c;将每个仓库在三台不同的、独立…

[转]Android ListView常用用法

本文转自:http://www.iteye.com/topic/540423 ListView是比较常用的控件&#xff0c;但一直都觉得创建ListView步骤有点繁琐&#xff0c;故在此总结一下&#xff0c;方便查阅。 程序效果是实现一个ListView,ListView里面有标题&#xff0c;内容和图片&#xff0c;并加入点击和长…

近期一些学习总结2

1.图片平铺 这个之前也用到过几次&#xff0c;一直没有总结&#xff1a; 第一种方法&#xff1a; #bo{ display:inline-block; width:100%;/*图片自适应的父容器的宽*/ height:100%;/*图片自适应的父容器的高*/ } #bo img{ width:100%; height:100%; } 第二种方法&am…

在Ubuntu的shell中启动eclipse且让它后台运行

2019独角兽企业重金招聘Python工程师标准>>> 在Ubuntu中&#xff0c;从命令行启动eclipse之后(比如执行./eclipse就启动eclipse了)&#xff0c;用于启动该eclipse的shell就会被阻塞&#xff0c;我们什么都做不了&#xff0c;直到该eclipse被关闭为止。 今天终于解决…