码迷,mamicode.com
首页 > 编程语言 > 详细

Python 画直方图

时间:2018-05-03 14:21:53      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:sub   python   code   limits   patch   show   ide   otto   its   

一个官方的例子,做直方图,用了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 画直方图

标签:sub   python   code   limits   patch   show   ide   otto   its   

原文地址:https://www.cnblogs.com/dajunma21/p/8984880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!