Python 画直方图
Posted dajunma21
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 画直方图相关的知识,希望对你有一定的参考价值。
一个官方的例子,做直方图,用了path patch的方法。貌似处理比较大的数据用的,后续再对比下另一个方法,暂记
import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.path as path fig, ax = plt.subplots() # Fixing random state for reproducibility np.random.seed(19680801) # histogram our data with numpy data = np.random.randn(1000) n, bins = np.histogram(data, 50) # get the corners of the rectangles for the histogram left = np.array(bins[:-1]) right = np.array(bins[1:]) bottom = np.zeros(len(left)) top = bottom + n # we need a (numrects x numsides x 2) numpy array for the path helper # function to build a compound path XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T # get the Path object barpath = path.Path.make_compound_path_from_polys(XY) # make a patch out of it patch = patches.PathPatch(barpath) ax.add_patch(patch) # update the view limits ax.set_xlim(left[0], right[-1]) ax.set_ylim(bottom.min(), top.max()) plt.show()
以上是关于Python 画直方图的主要内容,如果未能解决你的问题,请参考以下文章