2012-10-23 100 views
1

我有两列数据,x和y。 y数据呈现下面三角波的形状。正如你所看到的,三角形有2个正梯度部分和1个更长部分的负梯度。根据渐变的符号(Python)为数据分配颜色

Triangular shape of y-data (amplitude is not important)

我想写一个程序:

在垂直阵列的当前条目
  • 查询是否具有相对于阵列中的连续项的正或负梯度。
  • 然后,将y数据绘制为x,其中具有正梯度(及其各自的x值)的y值使用一种颜色绘制,负绘制使用另一种颜色绘制。

在Python中如何做到最好?

+0

用PIL或matplotlib。当您遇到特定问题时,请回来,我们很乐意为您提供帮助。 – lolopop

+1

我认为我已尽可能具体说明......如果您的意思是您希望我提供我的试用代码,那是可行的!我希望有不同的方法。 – 8765674

回答

1
filen = 'filename.txt' 
x = loadtxt(fn,unpack=True,usecols=[0]) 
y = loadtxt(fn,unpack=True,usecols=[1]) 

n = ma.masked_where(gradient(y) < 0, y) 
p = ma.masked_where(gradient(y) > 0, y) 

pylab.plot(x,n,'r',x,p,'g') 

对我来说是否有把戏!