
Change line colour depending on y axis value
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matthew
el 27 de Ag. de 2019
Comentada: Adam Danz
el 28 de Ag. de 2019
I have a graph with a blue line plotted that passes through a region around y=±2 (grey in the top image).
I want that line to change colour to also be grey only when it passes through this region (mockup in bottom image).
i.e. Above y=2 line is blue, between y=2 & y=-2 line is grey, below y=-2 line is blue. The format of the variable used to plot the line is also shown below.



0 comentarios
Respuesta aceptada
Adam Danz
el 27 de Ag. de 2019
Editada: Adam Danz
el 27 de Ag. de 2019
Since a line object cannot have multiple colors, you'll need to split the the (x,y) coordinates into 2 groups: within and outside the zone. Replace data with NaNs so that those section are not plotted.
Here's a demo
% Produce some curve & plot it
x = 0:1:3000;
gf = @(x, sigma, t, v) v*exp(-(((x-t).^2)/(2*sigma.^2)));
y = gf(x, 200, 1500, 40);
figure()
subplot(2,1,1)
plot(x,y)
title('All data')
% Define a y-axis window and determine which coordinates
% pass through the window
window = [10,20]; % y=10 to y=20
% Find the index of coordinates that are within the window
yInWindowIdx = y > window(1) & y < window(2);
% Plot all of the "out" data
subplot(2,1,2)
title('Segmented data')
hold on
xOut = x;
xOut(yInWindowIdx) = NaN;
yOut = y;
yOut(yInWindowIdx) = NaN;
plot(xOut, yOut, 'b-', 'LineWidth', 3)
% Plot all of the "in" data
xIn = x;
xInt(~yInWindowIdx) = NaN;
yIn = y;
yIn(~yInWindowIdx) = NaN;
plot(xIn, yIn, 'r-', 'LineWidth', 3)

6 comentarios
Adam Danz
el 28 de Ag. de 2019
The data isn't interpolated in the first place. Matlab connects the coordinates with lines but that's no interpolation. I agree with Adam that if you interpolate the data, that will address the problem.
Another solution would be make the first and last point within each "in-zone" segment a shared point such that those bookend points are in both lines. Then the blue like would extend into the "in-zone" until it touches the first in-point.
Más respuestas (1)
Stephen23
el 28 de Ag. de 2019
Editada: Stephen23
el 28 de Ag. de 2019
Method one: FEX
Some of these might do what you want:
https://www.mathworks.com/matlabcentral/fileexchange/39972-colormapline-color-changing-2d-or-3d-line?
etc.
Method two: multiple axes
Here is a straightforward method that does not require data interpolation or any data manipulation. It works by simply overlaying a second axes on top of the first, and plotting exactly the same data. Note how the colored lines are "cut off" at the boundaries, even though there are no data points there!
X = (0:0.1:1)*2*pi;
Y = sin(X);
ub = +0.4; % upper bound
lb = -0.3; % lower bound
fgh = figure();
ax1 = axes('parent',fgh); % background
ax2 = axes('parent',fgh); % foreground
plot(ax1,X,Y,'-*','Color',[0,0,1],'LineWidth',2);
plot(ax2,X,Y,'-*','Color',[1,0,0],'LineWidth',2);
ylm = get(ax1,'YLim');
pos = get(ax1,'Position');
pos(2) = pos(4)*((lb-ylm(1))/diff(ylm))+pos(2);
pos(4) = pos(4)*(ub-lb)/diff(ylm);
set(ax2, 'Position',pos, 'Visible','off', 'YLim',[lb,ub])
hold(ax1,'on')
plot(ax1,X([1,end]),[ub,ub],'-') % horizontal line
plot(ax1,X([1,end]),[lb,lb],'-') % horizontal line
Giving:

Using three or four axes would allow complete control over how the lines appear, e.g. if the (dotted) lines "show through" from the backround axes.
0 comentarios
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
