Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

barh

水平条形图

  • Horizontal bar graph

说明

示例

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' 样式的条形图支持设置条形属性。在所有其他输入参量之后指定名称-值对组参量。有关属性列表,请参阅 Bar 属性

示例

barh(ax,___) 在目标坐标区中显示条形图。将坐标区指定为上述任一语法中的第一个参量。

示例

b = barh(___) 返回一个或多个 Bar 对象。如果 y 是向量,则 barh 返回一个 Bar 对象。如果 y 是矩阵,则 barh 为每个序列返回一个 Bar 对象。显示条形图后,使用 b 设置条形的属性。

示例

全部折叠

创建一个包含四个值的向量。在一个条形图中显示这些值,每个水平条形对应一个值。

y = [10 20 30 41];
barh(y)

Figure contains an axes object. The axes object contains an object of type bar.

x 定义为一个包含三个年份值的矩阵。将 y 定义为包含四个城市的降雪数据的矩阵。在每年的组中显示四个条形序列。然后添加轴标签和图例。

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'})

Figure contains an axes object. The axes object with xlabel Snowfall, ylabel Year contains 4 objects of type bar. These objects represent Springfield, Fairview, Bristol, Jamesville.

自 R2023b 起

创建一个包含四个条形名称的字符串向量 x。创建一个包含条形长度的数值向量 y。然后创建一个 xy 的条形图。

x = ["Spring" "Summer" "Autumn" "Winter"];
y = [1 2 3 4];
barh(x,y)

Figure contains an axes object. The axes object contains an object of type bar.

创建矩阵 y。然后在条形图中以 25 为基线值显示 y 值。小于 25 的值显示在基线的左侧。

y = [8 15 33; 30 35 40; 50 55 62];
barh(y,'BaseValue',25)

Figure contains an axes object. The axes object contains 3 objects of type bar.

x 定义为一个包含三个年份值的向量。将 y 定义为包含负值和正值组合的矩阵。在堆叠水平条形图中显示值。

x = [1980 1990 2000];
y = [15 20 -5; 10 -17 21; -10 5 15];
barh(x,y,'stacked')

Figure contains an axes object. The axes object contains 3 objects of type bar.

y 定义为包含四个值的向量,并在水平条形图中显示值。然后调用 yticklabels 函数来更改垂直轴上的刻度标签。

y = [10 20 30 41];
barh(y)
yticklabels({'April','May','June','July'})

Figure contains an axes object. The axes object contains an object of type bar.

指示条形图类别的一种方法是将 X 指定为分类数组。barh 函数使用经过排序的类别列表,因此条形的显示顺序可能与您预期的有所不同。要保留顺序,请调用 reordercats 函数。

X 定义为分类数组,并调用 reordercats 函数来指定条形的顺序。然后将 Y 定义为条形长度的向量,并显示条形图。

X = categorical({'Small','Medium','Large','Extra Large'});
X = reordercats(X,{'Small','Medium','Large','Extra Large'});
Y = [10 21 33 52];
barh(X,Y)

Figure contains an axes object. The axes object contains an object of type bar.

vals 定义为包含两个数据序列的矩阵。在水平条形图中显示数据,并指定输出参量。由于有两个序列,barh 返回包含两个 Bar 对象的向量。

x = [1 2 3];
vals = [2 3 6; 11 23 26];
b = barh(x,vals);

Figure contains an axes object. The axes object contains 2 objects of type bar.

在第一个条形序列的末端将值作为标签显示。要实现此目的,请通过获取第一个 Bar 对象的 XEndPointsYEndPoints 属性来获取条形末端的坐标。由于水平条形图具有经过旋转的坐标区,因此在将 XEndPointsYEndPoints 的值传递给 text 函数之前,您必须对它们进行切换。将填充值 0.3 添加到 YEndpoints 中,使文本不会紧挨条形的边。然后调用 text 函数以显示标签。

xtips1 = b(1).YEndPoints + 0.3;
ytips1 = b(1).XEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'VerticalAlignment','middle')

Figure contains an axes object. The axes object contains 5 objects of type bar, text.

接下来,按照相同的步骤,在第二个条形序列的末端显示标签。

xtips2 = b(2).YEndPoints + 0.3;
ytips2 = b(2).XEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'VerticalAlignment','middle')

Figure contains an axes object. The axes object contains 8 objects of type bar, text.

创建数据并在水平条形图中显示数据,该条形图中条形的宽度为 0.4,颜色为红色。

y = [10 22 30 42];
width = 0.4;
barh(y,width,'red');

Figure contains an axes object. The axes object contains an object of type bar.

创建矩阵 y,其中每列为一个数据序列。然后在条形图中显示数据,指定在调用 barh 函数时的输出参量。在本例中,barh 返回包含三个 Bar 对象的向量。每个对象对应于一个不同序列。

y = [10 15 20; 30 35 40; 50 55 62];
b = barh(y);

Figure contains an axes object. The axes object contains 3 objects of type bar.

将第二个条形序列改为绿色,并使用加粗的红边。

b(2).FaceColor = [.2 .6 .5];
b(2).EdgeColor = [.63 .08 .18];
b(2).LineWidth = 2;

Figure contains an axes object. The axes object contains 3 objects of type bar.

创建数据,并使用使用默认的 'grouped' 样式和 'stacked' 样式在两个不同的条形图中显示数据。

x = [1980 1990 2000];
y = [8 15 25; 30 35 40; 50 55 62];

% Grouped
tiledlayout(2,1);
ax1 = nexttile;
barh(ax1,x,y)
title('Grouped Style')

% Stacked
ax2 = nexttile;
barh(ax2,x,y,'stacked')
title('Stacked Style')

Figure contains 2 axes objects. Axes object 1 with title Grouped Style contains 3 objects of type bar. Axes object 2 with title Stacked Style contains 3 objects of type bar.

输入参数

全部折叠

垂直轴坐标,指定为标量、向量、矩阵、字符串数组或字符向量元胞数组。x 的值不需要按顺序排列,但 x 的大小取决于 y 的大小以及您要如何显示数据。下表说明了最常见的情况。

表示形式如何指定 XY示例
显示一个条形序列。

指定 xy 为相同长度的向量。x 中的值必须唯一,但 y 中的值不需要唯一。

x = [1980 1990 2000];
y = [10 20 30];
barh(x,y)

Horizontal bar chart containing one series of bars. One blue bar is displayed at each location in x.

分组显示多个条形序列。

指定以下任意一种组合:

  • 指定 xy 为相同大小的矩阵。y 的每列对应一个条形序列。默认情况下,每个序列均为不同颜色。为确保各组的位置一致,请将 x 的列指定为相同的向量。即使各列重复,一个列中的值也必须唯一。

  • x 指定为唯一值的向量,并将 y 指定为矩阵。x 的长度必须等于 y 的至少一个维度的长度。y 的另一个维度包含不同条形序列的值。

x = [1980 1980 1980
     1990 1990 1990];
y = [2 6 9
    11 22 32];
barh(x,y)
x = [1980 1990];
y = [2 6 9
    11 22 32];
barh(x,y)

Horizontal bar chart containing three series of bars. Each location in x has a group of three bars. The first bar in each group is dark blue, the second bar is dark orange, and the third bar is dark yellow.

显示以一个 x 值为中心的一组条形。

指定 x 为标量,y 为向量。

x = 1990;
y = [10 20 30];
barh(x,y)

Horizontal bar chart containing one group of bars at the specified x location. The first bar is dark blue, the second bar is dark orange, and the third bar is dark yellow.

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration | string (自 R2023b 起) | cell (自 R2023b 起)

条形长度,指定为标量、向量或矩阵。y 的大小取决于 x 的大小以及您要如何显示数据。下表说明了最常见的情况。

表示形式如何指定 XY示例
显示一个条形序列。

指定 xy 为相同长度的向量。x 中的值必须唯一,但 y 中的值不需要唯一。

x = [1980 1990 2000];
y = [10 20 30];
barh(x,y)

Horizontal bar chart containing one series of bars. One blue bar is displayed at each location in x.

分组显示多个条形序列。

指定以下任意一种组合:

  • 指定 xy 为相同大小的矩阵。y 的每列对应一个条形序列。默认情况下,每个序列均为不同颜色。为确保各组的位置一致,请将 x 的列指定为相同的向量。即使各列重复,一个列中的值也必须唯一。

  • x 指定为唯一值的向量,并将 y 指定为矩阵。x 的长度必须等于 y 的至少一个维度的长度。y 的另一个维度包含不同条形序列的值。

x = [1980 1980 1980
     1990 1990 1990];
y = [2 6 9
    11 22 32];
barh(x,y)
x = [1980 1990];
y = [2 6 9
    11 22 32];
barh(x,y)

Horizontal bar chart containing three series of bars. Each location in x has a group of three bars. The first bar in each group is dark blue, the second bar is dark orange, and the third bar is dark yellow.

显示以一个 x 值为中心的一组条形。

指定 x 为标量,y 为向量。

x = 1990;
y = [10 20 30];
barh(x,y)

Horizontal bar chart containing one series of bars. One blue bar is displayed at each location in x.

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | duration

条形宽度,指定为每个条形可用空间的一定比例。默认值 0.8 表示每个条形使用从上一条形到下一条形之间的空间的 80%,两端各有 10% 的空白空间。

如果宽度为 1,则组中的条形会紧挨在一起。

示例: barh([1 2 3],0.5) 创建使用 50% 可用空间的条形。

分组样式,使用下列值之一指定。

样式结果示例

'grouped'

将每组显示为以对应的 x 值为中心的相邻条形。

Horizontal bar chart containing three series of bars. Each location in x has a group of three bars. The first bar in each group is dark blue, the second bar is dark orange, and the third bar is dark yellow.

'stacked'

将每组显示为一个多色条形。条形的长度是组中各元素之和。

如果 y 是向量,则结果与 'grouped' 相同。

Horizontal bar chart containing three series of bars that are stacked. Each location in x has one bar that has three different colored sections.

'histc'

以直方图格式显示条形,同一组中的条形紧挨在一起。每组的尾部边缘与对应的 x 值对齐。

注意

显示水平直方图的一个更好方法是调用 histogram 函数并指定 Orientation 名称-值对组参量。

Horizontal bar chart containing four series of bars in the histogram format. Each location in x has a group of four bars. The first bar in each group is dark blue, the second bar light blue, the third bar is green, and the fourth bar is yellow.

'hist'

以直方图格式显示条形。每组以对应的 x 值为中心。

注意

显示水平直方图的一个更好方法是调用 histogram 函数并指定 Orientation 名称-值对组参量。

Horizontal bar chart containing four series of bars in the histogram format. Each location in x has a group of four bars. The first bar in each group is dark blue, the second bar light blue, the third bar is green, and the fourth bar is yellow.

条形颜色,指定为下表中的选项之一。

颜色名称短名称外观
'red''r'

Sample of the color red

'green''g'

Sample of the color green

'blue''b'

Sample of the color blue

'cyan' 'c'

Sample of the color cyan

'magenta''m'

Sample of the color magenta

'yellow''y'

Sample of the color yellow

'black''k'

Sample of the color black

'white''w'

Sample of the color white

目标坐标区,指定为 Axes 对象。如果未指定坐标区,则条形图显示在当前坐标区中。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: barh([10 20 30],'EdgeColor','g') 将围绕条形的轮廓指定为绿色。

注意

  • 此处所列的属性只是一部分。有关完整列表,请参阅 Bar 属性

  • 您只能在使用默认 'grouped''stacked' 样式的条形图上设置这些属性。

轮廓颜色,指定为 'flat'、RGB 三元组、十六进制颜色代码、颜色名称或短名称。如果有 150 个条形或更少,则默认值为 [0 0 0],对应于黑色。如果相邻条形超过 150 个,则默认值为 'none'

从 R2017b 开始,'flat' 选项使用 CData 值对边进行着色。在以前的版本中,'flat' 选项使用颜色图中的颜色对边进行着色。

对于自定义颜色,请指定 RGB 三元组或十六进制颜色代码。

  • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

  • 十六进制颜色代码是字符串标量或字符向量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

颜色名称短名称RGB 三元组十六进制颜色代码外观
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"不适用不适用不适用无颜色

以下是 MATLAB® 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

示例: b = bar(1:10,'EdgeColor','red')

示例: b.EdgeColor = [0 0.5 0.5];

示例: b.EdgeColor = 'flat';

示例: b.EdgeColor = '#D2F9A7';

填充颜色,指定为 'flat'、RGB 三元组、十六进制颜色代码、颜色名称或短名称。'flat' 选项使用 Bar 对象的 CData 属性值对面进行着色。

对于自定义颜色,请指定 RGB 三元组或十六进制颜色代码。

  • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

  • 十六进制颜色代码是字符串标量或字符向量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

颜色名称短名称RGB 三元组十六进制颜色代码外观
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"不适用不适用不适用无颜色

以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

从 R2017b 开始,默认值是来自坐标区的 ColorOrder 属性的一个 RGB 三元组。在以前的版本中,默认值为 'flat',且颜色基于颜色图。

示例: b = bar(1:10,'FaceColor','red')

示例: b.FaceColor = [0 0.5 0.5];

示例: b.FaceColor = 'flat';

示例: b.FaceColor = '#D2F9A7';

条形轮廓的宽度,指定为以磅为单位的正值。一磅等于 1/72 英寸。

示例: 1.5

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

基线值,指定为数值标量值。

您指定的基线值将根据条形图的方向应用于 x 轴或 y 轴。如果您将条形图的方向由垂直更改为水平(或反之),基线值可能会改变。请在设置 Horizontal 属性之后设置 BaseValue 属性。

输出参量

全部折叠

Bar 对象。在创建具体的 Bar 对象后,可使用 b 中的元素访问和修改对象属性。Bar 对象的数量取决于 y 的大小。如果 y 是向量,则 b 是一个 Bar 对象。如果 y 是矩阵,则 b 是向量,其中包含的每个 Bar 对象都对应于 y 中一个序列。

详细信息

全部折叠

条形序列

一个序列由特定数据集在 X 中所有位置上的条形组成。默认情况下,每个条形序列由不同颜色指示。

条形组

一个组由 X 中一个特定位置上的所有条形组成。

扩展功能

版本历史记录

在 R2006a 之前推出

全部展开