Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - can you plot a histogram with a contour?

I want to plot a histogram with a contour like this enter image description here

I found this picture in here, but after following the same procedure there I don't get the contour.

I saw this question in stack overflow but it draws an edge over each bar, and I want only the outer contour.

How can I draw this outer contour? (I'm running python 3)

like image 992
bobsacameno Avatar asked Jan 28 '23 03:01

bobsacameno


1 Answers

The plot is probably produced with a different (i.e. older) matplotlib version. This can also be seen from the use of normed, which is deprecated in newer versions.

Here, you would explicitely set the edgecolor to black. ec="k", or longer, edgecolor="black".

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

kwargs = dict(histtype='stepfilled', alpha=0.3, density=True, bins=40, ec="k")

plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs);

plt.show()

enter image description here

like image 178
ImportanceOfBeingErnest Avatar answered Feb 09 '23 15:02

ImportanceOfBeingErnest