隐藏直方图 [英] Hide histogram plot
问题描述
所以我想绘制一个正态分布,并且我已经看到一种做到这一点的方法是使用以下代码:
So I want to plot a normal distribution, and I've seen one way to do this is by using this code:
import numpy as np
import matplotlib.pyplot as plt
mu = 5
sigma = 1
s = np.random.normal(mu, sigma, 1000)
count, bins, ignored = plt.hist(s, 100, normed=True);
pdf = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu)**2 / (2 * sigma**2))
mu_ = 10
sigma_ = 1
s = np.random.normal(mu_, sigma_, 1000)
count_, bins_, ignored_ = plt.hist(s, 100, normed=True);
pdf_ = 1/(sigma_ * np.sqrt(2 * np.pi)) * np.exp(- (bins_ - mu_)**2 / (2 * sigma_**2))
plt.plot(bins, pdf, linewidth=2, color='g')
plt.plot(bins_, pdf_, linewidth=2, color='r')
plt.show()
结果是:
我的问题是,我可以以某种方式隐藏直方图,以便仅显示正态分布线吗?我知道还有另一种绘制正态分布的方法,但是我有点喜欢这种方式
My question is, can I somehow hide the histogram plot so only the normal distribution line is shown?? I know there is another way to plot normal distribution, but I kinda prefer this way
谢谢您的帮助!
推荐答案
尝试在之前添加plt.clf()
:
plt.plot(bins, pdf, linewidth=2, color='g')
plt.plot(bins_, pdf_, linewidth=2, color='r')
这将清除直方图,同时仍允许您使用绘制的输出.如果您希望有两个单独的图形,一个带有直方图,另一个带有线条,请添加plt.figure()
而不是plt.clf()
.
This will clear the histogram, while still allowing you to use the output from it being drawn. If you'd like to have two separate figures, one with histogram and one with lines, add plt.figure()
instead of plt.clf()
.
这篇关于隐藏直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!