根据第三个值更改2D绘制线的颜色 [英] Change color of 2D plot line depending on 3rd value
问题描述
我有一个像这样的数据集
I have a data set that looks like this
140400 70.7850 1
140401 70.7923 2
140402 70.7993 3
140403 70.8067 4
140404 70.8139 5
140405 70.8212 3
其中第一列对应于时间(数据点之间的一秒间隔),并且将在x轴上,第二列对应于距离,并且将在y轴上。第三列是一个数字(一到五),这是一个运动的资格。
Where the first column corresponds to time (one second intervals between data points) and will be on the x axis, the second column corresponds with distance and will be on the y axis. The third column is a number (one through five) that is a qualification of the movement.
我想做一个图,改变两点之间的线的颜色这取决于前一个数据点的编号。例如,我想在第一个和第二个数据点之间的线是红色的,因为资格值为1.
I want to make a plot that changes the color of the line between two points depending on what the number of the previous data point was. For example, I want the line to be red between the first and second data points because the qualification value was 1.
我看到很多关于制作
我试着做一些事情,但是我只想要5种颜色:(红色,橙色,黄色,绿色和蓝色)像这样:
I tried doing something like this:
plot(x,y,{'r','o','y','g','b'})
但没有运气。
如何处理这个问题的任何想法?如果可能,不循环。
Any ideas of how to approach this? Without looping if possible.
推荐答案
你也可以使用一个技巧与Matlab版本前面2014b作为2009a至少)。
然而,永远不会像你预期的那么简单(除非你为这个解决方案写一个封装,你可以忘记 plot(x, y,{'r','o','y','g','b'})
You can also do it with a trick which works with Matlab version anterior to 2014b (as far back as 2009a at least).
However, is will never be as simple as you expected (unless you write a wrapper for one of the solution here you can forget about plot(x,y,{'r','o','y','g','b'})
).
是使用表面
而不是行
对象。表面受益于其 CData
属性和许多有用的功能来利用颜色贴图和纹理。
The trick is to use a surface
instead of a line
object. Surfaces benefit from their CData
properties and a lot of useful features to exploit color maps and texture.
Matlab surf
不处理1D数据,它需要一个矩阵作为输入,所以我们要通过复制每个坐标集(例如 xx = [x ,x]
)。
不要担心,表面会像线一样细,所以最终结果不难看。
Matlab surf
does not handle 1D data, it needs a matrix as input so we are going to give it by just duplicating each coordinate set (for example xx=[x,x]
).
Don't worry though, the surface will stay as thin as a line, so the end result is not ugly.
%% // your data
M=[140400 70.7850 1
140401 70.7923 2
140402 70.7993 3
140403 70.8067 4
140404 70.8139 5
140405 70.8212 3];
x = M(:,1) ; %// extract "X" column
y = M(:,2) ; %// same for "Y"
c = M(:,3) ; %// extract color index for the custom colormap
%% // define your custom colormap
custom_colormap = [
1 0 0 ; ... %// red
1 .5 0 ; ... %// orange
1 1 0 ; ... %// yellow
0 1 0 ; ... %// green
0 0 1 ; ... %// blue
] ;
%% // Prepare matrix data
xx=[x x]; %// create a 2D matrix based on "X" column
yy=[y y]; %// same for Y
zz=zeros(size(xx)); %// everything in the Z=0 plane
cc =[c c] ; %// matrix for "CData"
%// draw the surface (actually a line)
hs=surf(xx,yy,zz,cc,'EdgeColor','interp','FaceColor','none','Marker','o') ;
colormap(custom_colormap) ; %// assign the colormap
shading flat %// so each line segment has a plain color
view(2) %// view(0,90) %// set view in X-Y plane
colorbar
will get you:
作为一个更通用的例子:
As an example of a more general case:
x=linspace(0,2*pi);
y=sin(x) ;
xx=[x;x];
yy=[y;y];
zz=zeros(size(xx));
hs=surf(xx,yy,zz,yy,'EdgeColor','interp') %// color binded to "y" values
colormap('hsv')
view(2) %// view(0,90)
会给你一个正弦波与 y
值相关联的颜色:
will give you a sine wave with the color associated to the y
value:
这篇关于根据第三个值更改2D绘制线的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!