python - 根据梯度符号为数据分配颜色 (Python)

标签 python numpy plot gradient

我有两列数据,x 和 y。 y 数据采用下面三角波的形状。正如您所看到的,三角形有 2 个正梯度部分和 1 个较长的负梯度部分。

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

我想编写一个程序:

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

如何在 Python 中最好地完成此操作?

最佳答案

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

对我有用!

关于python - 根据梯度符号为数据分配颜色 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037432/

相关文章:

python - 如何通过 Python Selenium BeautifulSoup 从网站中提取文本形式的证券价格

python - 使用 Numpy 的矩阵解决方案 : no solution?

python - 从 numpy 数组中减去列

python - 基于python中的条件绘制多色线

python - 在基于 azure Flask 的 python 应用程序中提到文件路径的位置

python - Django-复数形式的无效 token : EXPRESSION

python - 如何为 Python3 安装 Bokeh

python - Cython:如何从 C 级类型获取 'actual Python type'(类型代码/dtype)

r - 如何更改 corrplot 的颜色、字体类型和大小?

python - 有没有办法分离 matplotlib 图以便计算可以继续?