I am using Anaconda 2.7 and my fill_between() attempts are coming up fruitless. I'm not sure if I'm missing a package or if my plotting syntax is throwing python off...
This is my code:
from scipy import stats
import matplotlib.pyplot as plt
from numpy import linspace
alpha_A = 11
beta_A = 41
alpha_B = 3
beta_B = 3
x = linspace(0,1,num = 1000)
postA = stats.beta(alpha_A, beta_A).pdf(x)
postB = stats.beta(alpha_B, beta_B).pdf(x)
plt.figure(2, figsize = (6,4))
plt.plot(postA, color = 'r', label = "A: Beta(" + str(alpha_A) + ',' + str(beta_A) + ')')
plt.plot(postB, color = 'b',label = "B: Beta(" + str(alpha_B) + ',' + str(beta_B) + ')')
plt.legend(loc = "best", frameon = False)
plt.fill_between(x, postA, facecolor = "red") # <---- not working
frame1 = plt.gca()
frame1.axes.get_xaxis().set_ticks([])
ax = plt.gca()
ax.set_xticks([0,200,400,600,800,1000])
ax.set_xticklabels( ['0.0','0.2','0.4','0.6','0.8','1.0']) # https://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#setting-tick-labels
ax.set_title("Posterior Distributions")
This gives me this graph, in which no red fill appears: 
It actually works, if you zoom on left hand side of your plot (you can see it on the image you show, the vertical line that goes up to 7).
So, why is that ?
It's because your plot goes up to 1000 on x axis, and you ask to fill it up to 1 (max(x)).
2 solutions :
The quick one :
You replace this line :
plt.fill_between(x, postA, facecolor = "red")
by this one
plt.fill_between(range(len(x)), postA, facecolor = "red")
The clean one :
where the second part of your code become :
fig = plt.figure(figsize = (6,4))
ax = fig.add_subplot(111)
ax.plot(x, postA, color = 'r', label = "A: Beta(" + str(alpha_A) + ',' + str(beta_A) + ')')
ax.plot(x, postB, color = 'b',label = "B: Beta(" + str(alpha_B) + ',' + str(beta_B) + ')')
ax.legend(loc = "best", frameon = False)
ax.fill_between(x, postA, facecolor = "red")
ax.set_title("Posterior Distributions")
Here you give the x value in your plot as being x, and not len(postA) if you give nothing else than Y value. So you'll have the right x-ticks directly.
Here the result with the clean solution :

Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With