How can I shade the area under the curve between two lines in matplotlib / pandas?

I am trying to recreate this diagram, more or less, using matplotlib:

enter image description here

With the exception of my center - 100, and the standard deviation - 16 (for example, IQ). This is what I have so far:

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats as stats
x = np.linspace(50,150,100)
iq = stats.norm.pdf(x, 100, 16)
plt.plot(x,iq)

This generates a normal curve as follows:

enter image description here

So far so good. But I don’t understand how to shade the areas under the curve.

+4
source share
1 answer

You can use plt.fill_between:

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

plt.style.use('ggplot')

mean = 100
std = 16

x = np.linspace(mean - 5 * std, mean + 5 * std, 1000)

iq = stats.norm(mean, std)

plt.plot(x, iq.pdf(x), 'r-', lw=3)

colors = ['c', 'r', 'b', 'g', ]
colors = colors + list(reversed(colors))

for i, color in zip(range(-4, 4), colors):
    low = mean + i * std
    high = mean + (i + 1) * std
    px = x[np.logical_and(x >= low, x <= high)]
    plt.fill_between(
        px,
        iq.pdf(px),
        color=color,
        alpha=0.5,
        linewidth=0,
    )

plt.tight_layout()
plt.savefig('test.png', dpi=300)

result

+7
source

All Articles