简体   繁体   中英

Color portion between two vertical lines on a graph in MATLAB

I have plotted a graph as below, plotted vertical lines at some specified X-axis points (which will be the results of my work), and I'm trying to color portion between two successive lines as shown in this graph.

在此处输入图片说明

If you want to reproduce this graph, that is, color different parts of the plot differently, this can be done by setting the LineSpec of the plot command appropriately. For example:

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

In the second plot command, we draw the part of x left of the separator in blue, the part right of the separator in green, and a vertical line at the separation in red. Look at the examples in the documentation for plot to see other possibilities of using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM