1

I want to plot the beta distribution with beta.pdf(x, a, b) but I only get the following error:

FloatingPointError: underflow encountered in exp.

This is my code:

x = np.linspace(0, 1, 990)
a=50.5
b=940.5

y = beta.pdf(x, a, b)

plt.title("PDF of Beta (Bell-shape)", fontsize=20)
plt.xlabel("X", fontsize=16)
plt.ylabel("Probability Density", fontsize=16)
plt.plot(x, y, linewidth=3, color='firebrick')
plt.annotate("Beta(50.5,940.5)", xy=(0.15, 2), size = 100, ha='center', va='center', color='firebrick')

fig = plt.gcf()
plt.show()


fig.savefig('my_plot.png',dpi=130)
Hagbard
  • 3,430
  • 5
  • 28
  • 64
Mohd Bilal
  • 101
  • 8

1 Answers1

1

I had the exact same problem while trying to create a seaborn violin plot. Thus, I updated the packages scipy, matplotlib, numpy and seaborn with the following commands:

pip install --upgrade scipy
pip install --upgrade matplotlib
pip install --upgrade numpy
pip install --upgrade seaborn

Unfortunately, that didn't help either, so I ended up using the seterr method of numpy to simply ignore this warning (as described in this old answer). You can set it as follows at the beginning of your code:

numpy.seterr(under='ignore')
Hagbard
  • 3,430
  • 5
  • 28
  • 64