这次介绍pyplot如何画直方统计图,并且画出最合适的轮廓线。这是Matplotlib自带的一个例子,示例程序example/pyplot_examples/histdiagram_demo.py。
先看效果图,

在看代码,
#!/usr/bin/env python import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt mu, sigma = 100, 15 x = mu + sigma*np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75) # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = plt.plot(bins, y, 'r--', linewidth=1) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()
Matplotlib很不错,不过暂时不会用到更多的功能,所以不会有更多的内容更新了。下一篇是关于输出eps格式。
本文详细介绍使用Python的Matplotlib库绘制直方图的方法,并通过示例代码展示如何添加最佳拟合线。从数据生成、绘图参数设置到图表美化,全面解析直方图绘制流程。
1182

被折叠的 条评论
为什么被折叠?



