MATLAB绘制水平的柱形图
水平方向的柱形图,可以更好的展现不同情景或对象之间的差异性,在论文数据绘图处理时,经常会使用到。MATLAB的barh()函数可以绘制水平的柱形图。
1.barh函数 语法 barh(y) barh(x,y) barh(___,width) barh(___,style) barh(___,color) barh(___,Name,Value) barh(ax,___) b=barh(___) 说明 barh(y) 创建一个水平条形图,每个条形对应 y 中一个元素。 如果 y 是 m×n 矩阵,则 barh 创建每组包含 n 个条形的 m 个组。 barh(x,y) 沿垂直轴在 x 指定的位置绘制条形。 barh(___,width) 指定每个条形占用的可用空间比例。例如,barh(y,1) 让每组中的条形紧挨在一起。 将 width 指定为上述任一语法中的最后一个参数。 barh(___,style) 指定条形组的样式。例如,barh(y,"stacked") 将每组中的条形堆叠成一个多色条形。 barh(___,color) 为所有条形指定单一颜色。例如,barh(y,"red") 显示红色条形。 barh(___,Name,Value) 使用一个或多个名称-值对组参数指定条形图的属性。 仅使用默认 "grouped" 或 "stacked" 样式的条形图支持设置条形属性。 在所有其他输入参数之后指定名称-值对组参数。 barh(ax,___) 在目标坐标区中显示条形图。将坐标区指定为上述任一语法中的第一个参数。 b=barh(___) 返回一个或多个 Bar 对象。如果 y 是向量,则 barh 返回一个 Bar 对象。 如果 y 是矩阵,则 barh 为每个序列返回一个 Bar 对象。显示条形图后,使用 b 设置条形的属性。
2.简单示例
程序clc; clear all; close all; y = [11 12 13 14 12 28]; barh(y); figure; x = [1980 1990 2000]; y = [40 50 63 52; 42 55 50 48; 30 20 44 40]; barh(x,y) xlabel("Snowfall") ylabel("Year") legend({"Springfield","Fairview","Bristol","Jamesville"})
运行结果
3.修改中间的基准线
程度clc; clear all; close all; figure; data1 = [8 15 33; 30 35 40; 50 55 62]; barh(data1,"BaseValue",30) %显示具有负数据的堆叠条形 figure; x = [1980 1990 2000]; y = [15 20 -5; 10 -17 21; -10 5 15]; barh(x,y,"stacked") %自定义垂直轴刻度标签 figure; y = [10 20 30 41]; barh(y) yticklabels({"April","May","June","July"})
运行结果
指示条形图类别的一种方法是将 X 指定为分类数组。barh 函数使用经过排序的类别列表,因此条形的显示顺序可能与您预期的有所不同。要保留顺序,请调用 reordercats 函数。将 X 定义为分类数组,并调用 reordercats 函数来指定条形的顺序。然后将 Y 定义为条形长度的向量,并显示条形图。
程序clc; clear all; close all figure; X = categorical({"Small","Medium","Large","Extra Large"}); X = reordercats(X,{"Small","Medium","Large","Extra Large"}); Y = [10 21 33 52]; barh(X,Y)
运行结果
4.指定条形宽度和颜色
程序clc; clear all; close all figure; y = [10 22 30 42]; width = 0.4; barh(y,width,"red"); figure; y = [10 15 20; 30 35 40; 50 55 62]; b = barh(y); b(2).FaceColor = [.2 .6 .5]; b(2).EdgeColor = [.63 .08 .18]; b(2).LineWidth = 2;
运行结果
5.比较不同的条形样式
程序clc; clear all; close all; x = [1980 1990 2000]; y = [8 15 25; 30 35 40; 50 55 62]; % Grouped figure; subplot(2,1,1) barh(x,y) title("Grouped Style") % Stacked subplot(2,1,2) barh(x,y,"stacked") title("Stacked Style")
运行结果
6.实际例子
程序clc; clear all; close all; c=randi(1000,8); fenzhixianshi={"内存占用百分比";"冗余状况";"通信质量";"工作负荷";"卡件模块状态";"I/O通道状态";"故障信息";"总体分值"}; m=8;%数据数目 n=20;%数据次数 labels = fenzhixianshi(1:end,1); %每个数据的名字 rng(16,"twister") % 数据对应的颜色 color_map = randi(350,m,8) / 255; % 创建GIF文件 h1=figure("color","w"); %设置背景色 set(gcf, "position", [100,100,1300,600]); F = getframe(gcf); %捕获指定矩形区域,返回一个结构体 [A,map] = rgb2ind(frame2im(F),256); imwrite(A,map,"moveimage1.gif","LoopCount",inf,"DelayTime",0.001); for i = 1:8 % 按行取出每次的数据 tmp = c(i,:); % 绘制条形图 fig = barh(tmp, 0.5, "EdgeColor", "w","FaceColor","flat"); % 设置每个条形的颜色 fig.CData = color_map(8,:); % 设置标签 set(gca, "yticklabel",fenzhixianshi,"FontName", "Microsoft YaHei UI","FontWeight", "normal","XLim",[0 100],"FontSize", 12); xlabel("相应分值") title("示例"); axis([0 max(max(c))*1.1 0 9]) % 保存GIF文件 F = getframe(gcf); [A, map] = rgb2ind(frame2im(F), 256); if i == 1 imwrite(A, map, "moveimage1.gif","WriteMode","append","DelayTime",2); elseif i == n imwrite(A, map, "moveimage1.gif","WriteMode","append","DelayTime",5); else imwrite(A, map, "moveimage1.gif","WriteMode","append","DelayTime",2); end end
运行结果
参考内容
[1]https://ww2.mathworks.cn/help/matlab/ref/barh.html
作者:郭志龙
编辑:郭志龙
校对:郭志龙