gboffi
gboffi

Reputation: 25093

Different colors for different value ranges

I have two arrays, t and x, t is the independent variable and x is computed as a function of t.

I can plot them easily, e.g.,

from pylab import *
t = linspace(o, 2*pi, 201)
x = sin(t)
plot(t, x)

but what I'd like is different, because in my field when abs(x)>0.9 the probability of failure increases from 0.000001% to 99.999999% I'd like to plot in bright red the parts of the curve where I have the probable failure of my system.

I can imagine two possible solutions.

but I don't know if there are different, better possibilities and however I don't know how to implement a solution...

Upvotes: 2

Views: 1129

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

Matplotlib's plot function does not support colormaps. This leaves us with option "plot two masked arrays" - which is a good one.

The easiest option is to plot a new line on top of the complete plot which only contains the points that satisfy a condition.

import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt

x = np.arange(100)
y = np.abs(np.cumsum(np.random.rand(100)-0.5))/4.
y1 = np.copy(y)
y1[y1 < 0.7] = np.nan

plt.plot(x,y, linewidth=1.4)
plt.plot(x,y1, linewidth=2)

plt.show()

enter image description here

Upvotes: 5

Related Questions