Subject: color is more than color

In the plot, how can I color all values ​​above the threshold in a different color? Like everything above average + std or mean + 2 * std?

+1


source to share


1 answer


Usage LineCollection

is the correct way, but you can also do the light version in one line of code by masking the arrays:

enter image description here

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

      



The cheat here is that it draws the original data with the filtered data, so sometimes the base curve may be displayed depending on how it is displayed. I've made the red line a little thicker here, but I'm not sure if that has changed. The advantage is that there is basically one line,, ma.masked_less(y, 1.1)

for the threshold 1.1

.

Masked arrays are needed because otherwise there will be a line connecting the different segments and the mask causes these points to not be displayed.

+1


source







All Articles