情节:颜色都大于不同的颜色 [英] Plot: color all larger than different color
问题描述
在绘图中,如何将高于阈值的所有值着色为不同的颜色?就像上面的所有东西 mean + std 或 mean + 2*std ?
In a plot, how can I color all values above a threshold in a different color? Like everything above mean + std or mean + 2*std ?
推荐答案
使用 LineCollection
是正确的方法,但您也可以使用掩码数组在一行代码中完成一个简单的版本:
Using a LineCollection
is the proper way to go, but you can also do an easy version in one line of code using masked arrays:
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
# make a weird continuous function
r, t = np.random.random((100,)), np.arange(0, 100, .01)
y = sum(r[3*i+0]*np.sin(r[3*i+1]*t + 10*r[3*+2]) for i in range(10))
# generate the masked array
mask = ma.masked_less(y, 1.1)
plt.plot(t, y, 'k', linewidth=3)
plt.plot(t, mask, 'r', linewidth=3.2)
plt.show()
这里的作弊是它使用过滤后的数据绘制原始数据,因此有时可以显示底层曲线,具体取决于它的呈现方式.我把这里的红线加粗了一点,但我不确定它是否有所作为.优点是它基本上是一行,ma.masked_less(y, 1.1)
,阈值为1.1
.
The cheat here is that it draws over the original data with the filtered data so sometimes the underlying curve can show, depending on how it's rendered. I made the red line here a bit thicker, but I'm not sure whether it made a difference. The advantage is that it's basically one line, ma.masked_less(y, 1.1)
, for a threshold of 1.1
.
这里需要掩码数组的原因是,否则会有一条线连接不同的段,而掩码会导致这些点不会被绘制.
The reason masked arrays are needed here is that otherwise there would be a line connecting the different segments, and the mask causes these points to not be plotted.
这篇关于情节:颜色都大于不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!