【问题标题】:Color portion between two vertical lines on a graph in MATLABMATLAB中图表上两条垂直线之间的颜色部分
【发布时间】:2014-06-03 16:52:54
【问题描述】:

我绘制了如下图,在某些指定的 X 轴点处绘制了垂直线(这将是我的工作结果),并且我正在尝试为两条连续线之间的部分着色,如图所示。

【问题讨论】:

  • 问题是什么?你想重现这个图表吗?

标签: matlab graph colors plot


【解决方案1】:

如果您想重现此图,即对图的不同部分进行不同的着色,可以通过适当设置plot 命令的LineSpec 来完成。例如:

x = 0:pi/100:2*pi;
y = sin(x);
% This plots a regular, one color graph
figure;
plot(x, y);
% This plots several parts of the graph differently, and sets a vertical line between them
xSeparator = pi;
leftside = x < xSeparator;
figure;
plot(x(leftside), y(leftside), 'b',...
    x(~leftside), y(~leftside), 'g',...
    [xSeparator xSeparator], [min(y) max(y)], 'r');

在第二个plot 命令中,我们将分隔符左侧的x 部分绘制为蓝色,将分隔符右侧的部分绘制为绿色,并将分隔符处的垂直线绘制为红色。查看plot 文档中的示例,了解使用它的其他可能性。

【讨论】:

  • 感谢您的精彩回复