绘制直方图

时间:2016-09-17 00:43:48

标签: python matplotlib graph histogram

我正在尝试用mu和sigma绘制直方图。

我试图在y轴上使用ec_scores值,它应该显示0.1到1.0 它在y轴上给我1,2,3,4,5,6。 我没有得到任何错误,但这完全抛弃了图表。请帮助我并告诉我我做错了什么以及如何才能正确生成图表。 谢谢。

这是我的代码:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.mlab as mlab

x = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])

ec_scores = np.array([1., 1., 1., 0.95923677, 0.94796184, 1., 0.76669558, 1., 0.99913194, 1.])

mu, sigma = (np.mean(ec_scores), np.std(ec_scores))


fig = plt.figure()

ax = fig.add_subplot(111)

n, bins, patches = ax.hist(x, 50, normed=1, facecolor='blue', alpha=0.75)


bincenters = 0.5*(bins[1:]+bins[:-1])

y = mlab.normpdf( bincenters, mu, sigma)

l = ax.plot(bincenters, y, 'r--', linewidth=1)

ax.set_xlabel('Parameters')

ax.set_ylabel('EC scores ')

plt.plot(x, ec_scores)

ax.grid(True)

plt.show()

目前图表如下所示: enter image description here

1 个答案:

答案 0 :(得分:1)

如@benton在评论中所述,您可以将ec_scores绘制为barchart

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
ec_scores = np.array([1., 1., 1., 0.95923677, 0.94796184, 1., 0.76669558, 1., 0.99913194, 1.])
mu, sigma = (np.mean(ec_scores), np.std(ec_scores))

fig = plt.figure()
ax = fig.add_subplot(111)
rects = ax.bar(x, ec_scores, width=0.1, align='center', facecolor='blue', alpha=0.75)

ax.set_xlabel('Parameters')
ax.set_ylabel('EC scores ')
ax.grid(True)

plt.show()

这将是这样的: Plotted as a barplot.

您还可以通过将数组传递给yerr参数来添加错误栏。这在我上面链接的条形图示例中说明。我不确定你究竟要用normpdf做什么,但是条形图会返回一个矩形列表而不是bin,所以你可能需要根据代码进行调整。

我希望这会有所帮助。