Main Content

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

bar

条形图

  • Bar graph

说明

示例

bar(y) 创建一个条形图,y 中的每个元素对应一个条形。

  • 要绘制单个条形序列,请将 y 指定为长度为 m 的向量。这些条形沿 x 轴从 1 到 m 依次放置。

  • 要绘制多个条形序列,请将 y 指定为矩阵,每个序列对应一列。

示例

bar(x,y)x 指定的位置绘制条形。

示例

bar(___,width) 设置条形的相对宽度以控制中各个条形的间隔。将 width 指定为标量值。可将此选项与上述语法中的任何输入参量组合一起使用。

示例

bar(___,style) 指定条形组的样式。例如,使用 'stacked' 将每个组显示为一个多种颜色的条形。

示例

bar(___,color) 设置所有条形的颜色。例如,使用 'r' 表示红色条形。

示例

bar(___,Name,Value) 使用一个或多个名称-值对组参量指定条形图的属性。仅使用默认 'grouped''stacked' 样式的条形图支持设置条形属性。在所有其他输入参量之后指定名称-值对组参量。有关属性列表,请参阅 Bar 属性

示例

bar(ax,___) 将图形绘制到 ax 指定的坐标区中,而不是当前坐标区 (gca) 中。选项 ax 可以位于上述语法中的任何输入参量组合之前。

示例

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

示例

全部折叠

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y)

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

指定沿 x 轴的条形位置。

x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)

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

自 R2023b 起

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

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

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

将各条形的宽度设置为各条形可用总空间的 40%。

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,0.4)

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

显示四个条形组,每一组包含三个条形。

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)

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

为矩阵中的每一行显示一个条形。每个条形的高度是行中各元素之和。

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')

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

创建一个标量 x 和一个向量 y。以 x=2020 为中心显示一个堆叠条形图。堆叠中的每个分块对应于 y 的一个元素。

 x = 2020; 
 y = [30 50 23]; 
 b = bar(x,y,"stacked");

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

调整堆叠条形图的宽度。由于各个条形是堆叠的,因此更改 Bar 对象的宽度会更改所有条形。

 b(1).BarWidth = 0.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];
bar(x,y,'stacked')

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

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

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

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

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

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

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

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

在第一个条形序列的末端显示值。通过获取第一个 Bar 对象的 XEndPointsYEndPoints 属性,获取条形末端的坐标。将这些坐标传递给 text 函数,并指定垂直和水平对齐方式,让值显示在条形末端上方居中处。

xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom')

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

接下来,在第二个条形序列的末端上方显示值。

xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom')

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

自 R2019b 开始提供

您可以使用 tiledlayoutnexttile 函数显示分块条形图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。在顶部坐标区中显示条形图。在底部坐标区中,显示相同数据的堆叠条形图。

y = [1 2 3; 4 5 6];
tiledlayout(2,1)

% Top bar graph
ax1 = nexttile;
bar(ax1,y)

% Bottom bar graph
ax2 = nexttile;
bar(ax2,y,'stacked')

Figure contains 2 axes objects. Axes object 1 contains 3 objects of type bar. Axes object 2 contains 3 objects of type bar.

使用红色条形创建一个条形图。

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'r')

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

使用 RGB 三元组设置条形内部颜色和轮廓颜色。设置条形轮廓的宽度。

y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)

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

可以使用 Bar 对象的 CData 属性控制单个条形的颜色。

创建一个条形图并将 Bar 对象赋给一个变量。将 Bar 对象的 FaceColor 属性设置为 'flat',从而使条形图使用 CData 属性中定义的颜色。默认情况下,CData 属性预先填充由默认 RGB 颜色值组成的矩阵。要更改特定的颜色,请更改矩阵中的对应行。例如,更改第二个条形的颜色。

b = bar(rand(10,1));
b.FaceColor = 'flat';
b.CData(2,:) = [.5 0 .5];

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

通过将 FaceColor 属性设置为 'flat',创建一个使用颜色图颜色的条形图。然后将每个 Bar 对象的 CData 属性设置为一个整数。

y = [1 3 5; 3 2 7; 3 4 2];
b = bar(y,'FaceColor','flat');
for k = 1:size(y,2)
    b(k).CData = k;
end

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

自 R2023b 起

使用命名的调色板可以方便地更改图的颜色。此示例使用三种不同的调色板来对条形图进行比较。

使用默认调色板创建一个随机数的条形图。

bar(rand(3,5))

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

使用 colororder 函数将调色板更改为 reef

colororder("reef")

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

将调色板更改为 earth

colororder("earth")

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

创建矩阵 y,其中每列为一个数据序列。调用 bar 函数以在条形图中显示数据,并指定输出参量。输出是包含三个 Bar 对象的向量,其中每个对象对应一个不同序列。无论条形是分组还是堆叠,均为如此。

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

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

将第三个条形序列设置为绿色。

b(3).FaceColor = [.2 .6 .5];

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

输入参数

全部折叠

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

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

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

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

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];
bar(x,y)
x = [1980 1990];
y = [2 6 9
    11 22 32];
bar(x,y)

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];
bar(x,y)

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 坐标,指定为标量、向量或矩阵。y 的大小取决于 x 的大小以及您要如何显示数据。下表说明了最常见的情况。

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

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

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

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];
bar(x,y)
x = [1980 1990];
y = [2 6 9
    11 22 32];
bar(x,y)

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];
bar(x,y)

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 | duration

条形宽度,指定为可用于每个条形的总空间的一部分。默认值 0.8 表示条形宽度是从上一条形到下一条形之间的空间的 80%,两端各占该空间的 10%。

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

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

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

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

样式结果示例

'grouped'

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

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' 相同。

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 函数。

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 函数。

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

坐标区对象。如果您不指定坐标区,则 bar 使用条形图的当前坐标区。

名称-值参数

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

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

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

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

注意

  • 此处所列的属性只是一部分。有关完整列表,请参阅 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';

颜色数据,指定为下列值之一:

  • RGB 三元组 - 对所有条形应用同一个 RGB 颜色值。

  • 三列矩阵 - 每个条形一种颜色。矩阵中的每一行指定一个特定条形的 RGB 三元组。

  • 标量 - 对所有条形应用颜色图中的一种颜色。

  • 向量 - 每个条形一种颜色。颜色来自颜色图。

默认情况下,当您创建条形图时,CData 属性包含一个由 RGB 三元组组成的三列矩阵。通过更改矩阵中的对应行,可以更改特定条形的颜色。

仅当 FaceColorEdgeColor 属性设置为 'flat' 时,此属性才适用。

示例

通过将 FaceColor 属性设置为 'flat',更改特定条形的颜色。然后将 CData 矩阵中的对应行更改为新的 RGB 三元组。例如,更改第二个条形的颜色。

b = bar(1:10,'FaceColor','flat');
b.CData(2,:) = [0 0.8 0.8];

Bar chart that has all dark blue bars except the second bar, which is cyan.

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

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

条形轮廓的线型,指定为此表中的线型之一。

线型描述表示的线条
"-"实线

Sample of solid line

"--"虚线

Sample of dashed line

":"点线

Sample of dotted line

"-."点划线

Sample of dash-dotted line, with alternating dashes and dots

"none"无线条无线条

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

示例: 1.5

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

输出参量

全部折叠

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

详细信息

全部折叠

条形序列

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

条形组

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

扩展功能

版本历史记录

在 R2006a 之前推出

全部展开