首页 > 解决方案 > 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)

标签: pythonmatplotlibhistogramdraw

解决方案


该图可能是使用不同的(即较旧的)matplotlib 版本生成的。这也可以从 的使用中看出,normed在较新的版本中已弃用。

在这里,您将明确地将边缘颜色设置为黑色。 ec="k", 或更长, 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()

在此处输入图像描述


推荐阅读