This code, plots a normal distribution curve:
import scipy as sp, numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
plt.rc('text', usetex=True)
x = np.arange(-6, 6, 0.1)
distrib_1 = norm(0, 1)
y_distrib_1 = distrib_1.pdf(x)
fig = plt.figure(figsize=(12, 8))
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x, y_distrib_1, color='red', linewidth=2, label=r'Normal distribution: $\mu = 0, \sigma = 1$')
axes.set_xlabel('x')
axes.set_ylabel('P(x)')
axes.set_title('The Normal Distribution')
axes.grid(True)
axes.legend(loc=2);
plt.show()
Now I want to fill the range between x=-1 and x=1. What I tried to do is axes.fill_between(x, y_distrib_1, 0, where = -1<x<1)
but then I get the error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
at that line.
I tried axes.fill_between(x, y_distrib_1, 0, where = (-1<x and x<1), color='red')
and it also didn't work.
What I can do is:
p = [(True if -1<val and val<1 else False) for val in x]
axes.fill_between(x, y_distrib_1, 0, where = p)
that works, but this code feels quite ugly.
What to do? I didn't find any similar example in the matplotlib documentation.