Python keeps overwriting hist on previous plot but doesn't save it with the desired plot

强颜欢笑 提交于 2019-12-02 07:58:06

This is happening, because you have set normed = True, which means that area under the histogram is normalized to 1. And since your bins are very wide, this means that the actual height of the histogram bars are very small (in this case so small that they are not visible)

If you use

n, bins, _ = plt.hist(data, bins = np.linspace(data[0], data[-1], 100), normed=True, alpha= 1)

n will contain the y-value of your bins and you can confirm this yourself.
Also have a look at the documentation for plt.hist.

So if you set normed to False, the histogram will be visible.

Edit: number of bins

import numpy as np
import matplotlib.pyplot as plt

rand_data = np.random.uniform(0, 1.0, 100)

fig = plt.figure()

ax_1 = fig.add_subplot(211)
ax_1.hist(rand_data, bins=10)

ax_2 = fig.add_subplot(212)
ax_2.hist(rand_data, bins=100)

plt.show()

will give you two plots similar (since its random) to:

which shows how the number of bins changes the histogram. A histogram visualises the distribution of your data along one dimension, so not sure what you mean by number of inputs and bins.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!