Main Content

控制绘制中颜色和线型的自动选择

当您在同一坐标区中绘制多个数据集时,MATLAB® 可帮助您通过更改数据集的外观来区分它们。例如,当您绘制多个线条时,MATLAB 会根据线条的创建顺序从一组颜色中为每个线条指定一种颜色。如果创建的线条数多于颜色数,MATLAB 会从第一种颜色开始循环使用各颜色。您可以更改此行为,例如:

本主题说明如何配置线图和散点图,但控制颜色循环(可能还有线型循环)的相同概念也适用于许多其他图,包括 bar 图、area 图和 stem 图等。本主题中的所有示例均在绘图之后设置坐标区属性。此步骤顺序非常重要,因为大多数绘图函数会重置许多坐标区属性。

注意

如果要为绘图中的某个特定项指定颜色或线型,请参阅指定绘图颜色指定绘图中的线和标记的外观

指定不同的颜色集和线型集

MATLAB 选择的颜色来自坐标区的 ColorOrder 属性,该属性包含一个由指定为 RGB 三元组的颜色组成的三列矩阵。RGB 三元组是三元素向量,包含颜色的红、绿和蓝分量的强度。强度必须在 [0, 1] 范围内。

如果绘制多个线条,则第一个线条使用 ColorOrder 矩阵中的第一种颜色,第二个线条使用第二种颜色,以此类推。最终,如果绘图中的线条数多于矩阵中的行数,颜色会循环重复使用。以下代码会创建几个使用默认色序的线图。第一个线条是最顶部的线条。由于默认 ColorOrder 矩阵有七行,因此颜色在第七个线条后开始循环。

plot([9 10 11 12])
hold on
plot([8 9 10 11])
plot([7 8 9 10])
plot([6 7 8 9])
plot([5 6 7 8])
plot([4 5 6 7])
plot([3 4 5 6])
plot([2 3 4 5])
plot([1 2 3 4])
hold off
legend("Location","northeastoutside")

Plot containing nine solid lines with a legend. The first seven lines have unique colors. The last two lines use the same colors as the first two lines.

您可以通过以下两种方式之一来更改颜色:

  • 将坐标区的 ColorOrder 属性设置为一个新的 RGB 三元组矩阵。

  • 调用 colororder 函数。此函数接受 RGB 三元组、颜色名称(如 "red")和十六进制颜色代码 (自 R2019b 起)它还接受几个预定义调色板名称中的任一个,如 "gem""reef""meadow" (自 R2023b 起)

创建一个包含表示红色、绿色和蓝色的 RGB 三元组的新矩阵。然后将 ColorOrder 属性设置为该矩阵。绘图会立即更新为新颜色。

mycolors = [1 0 0; 0 1 0; 0 0 1];
ax = gca; 
ax.ColorOrder = mycolors;

Plot containing nine solid lines with a legend. The first three lines have unique colors. The colors are repeated two more times for the last six lines.

除了循环使用颜色之外,MATLAB 还可以循环使用不同的线型。默认情况下,只有一种线型(实线)。要指定其他线型,请设置坐标区的 LineStyleOrder 属性。例如,以下代码会指定三种线型。更新后的绘图对一种线型循环使用所有颜色后,再开始使用下一种线型。

mylinestyles = ["-"; "--"; "-o"];
ax.LineStyleOrder = mylinestyles;

Plot containing three solid lines, three dashed lines, and three solid lines with circular markers. The three colors are repeated for each set of three lines. The plot also has a legend.

指定颜色和线型的循环顺序

自 R2023a 起

使用多种颜色和线型时,您可以通过设置 LineStyleCyclingMethod 属性来指定绘图是先循环使用所有线型再循环使用所有颜色、先循环使用所有颜色再循环使用所有线型还是同时循环使用两者。此属性包含以下三个值:

  • "aftercolor" - 先循环使用颜色再循环使用线型。这是默认值。

  • "beforecolor" - 先循环使用线型再循环使用颜色。

  • "withcolor" - 循环使用所有颜色的同时循环使用所有线型。

绘制四个线条。将 LineStyleOrder 属性设置为三种线型,并通过将一个由三个十六进制颜色代码组成的数组传递给 colororder 函数将 ColorOrder 属性设置为三种颜色。然后,添加一个图例。

% Plot four lines
plot([4 5 6 7])
hold on
plot([3 4 5 6])
plot([2 3 4 5])
plot([1 2 3 4])
hold off

% Set the line style order and color order
ax = gca;
ax.LineStyleOrder = ["-"; "--"; "-o"];
colororder(["#8040E6";"#1AA640";"#E68000"])
legend("Location","northeastoutside")

Plot containing four lines with three colors and two line styles. The first three lines are solid with unique colors. The last line is dashed and the has the same color as the first solid line. The plot also has a legend.

默认情况下,绘图在对第一种(实线)线型循环使用所有颜色之后,才显示下一种(虚线)线型。由于有三种颜色,因此在包含四个线条的绘图中只使用两种线型。

如果您希望更多线条的颜色和线型都不同,请使用 "withcolor" 选项,并指定相同数量的颜色和线型。例如,将前面绘图的 LineStyleCylingMethod 更改为 "withcolor"。更新后的绘图会同时循环使用颜色和线型。

ax.LineStyleCyclingMethod = "withcolor";

Plot containing four lines with three unique colors and three unique line styles. The last line has the same color and line style as the first line. The plot also has a legend.

按颜色或线型对数据进行分组

自 R2020a 起

要将相关的线条(或其他绘图对象)直观地组合在一起,请将您希望获得相同视觉效果的每个对象的 SeriesIndex 属性设置为相同的数字。SeriesIndex 属性根据 LineStyleCyclingMethod 属性的值对 ColorOrderLineStyleOrder 数组进行索引。因此,具有相同 SeriesIndex 值的不同对象将使用相同的颜色(和线型,如果适用)。

例如,绘制两组散点,每组包含 50 个点。

x = 1:50;
meas1 = 0.25*x + randn(1,50);
scat1 = scatter(x,meas1);
hold on
meas2 = 0.5*x + randn(1,50) + 5;
scat2 = scatter(x,meas2);

Two sets of scattered points that are offset from each other vertically. The top set of points is red, and the bottom set of points is blue.

使用 polyfitpolyval 函数为每组点计算一条拟合线。然后将每条拟合线添加到绘图中。

% Calculate fit lines for each set of measurements
p1 = polyfit(x,meas1,1);
y1_fit = polyval(p1,x);
p2 = polyfit(x,meas2,1);
y2_fit = polyval(p2,x);

% Plot fit lines
fitline1 = plot(x,y1_fit);
fitline2 = plot(x,y2_fit);
hold off

Plot of two sets of scattered points with fit lines that run through them. Each set of points and corresponding fit line are vertically displaced from the other set of points and fit line. The top set of points is red and the fit line is purple. The bottom set of points is blue and the fit line is yellow.

散点图和拟合线均采用不同的颜色。原因是 MATLAB 从 ColorOrder 矩阵中为每个添加到坐标区的新绘图选择了一种新颜色。

将每条拟合线的颜色与其相关联的散点图匹配。将每个线条的 SeriesIndex 属性设置为与相关联的散点图具有相同的值。

fitline1.SeriesIndex = scat1.SeriesIndex;
fitline2.SeriesIndex = scat2.SeriesIndex;

Plot of two sets of scattered points with fit lines that run through them. Each set of points and corresponding fit line are vertically displaced from the other set of points and fit line. The top set of points and fit line are both red. The bottom set of points and fit line are both blue.

即使您更改 ColorOrder 矩阵中的颜色,这种关系仍会存在。例如,通过调用 colororder 函数将颜色更改为紫色和绿色。

colororder([0.5 0.25 0.90; 0.10 0.65 0.25])

Plot of two sets of scattered points with fit lines that run through them. Each set of points and corresponding fit line are vertically displaced from the other set of points and fit line. The top set of points and fit line are both green. The bottom set of points and fit line are both purple.

另请参阅

函数

属性

相关主题