📄 histogram_demo_canvasagg.py
字号:
#!/usr/bin/env python"""This is an example that shows you how to work directly with the aggfigure canvas to create a figure using the pythonic API.In this example, the contents of the agg canvas are extracted to astring, which can in turn be passed off to PIL or put in a numericarray"""from matplotlib.backends.backend_agg import FigureCanvasAggfrom matplotlib.figure import Figurefrom matplotlib.axes import Subplotfrom matplotlib.mlab import normpdffrom matplotlib.numerix.mlab import randnfig = Figure(figsize=(5,4), dpi=100)ax = fig.add_subplot(111)canvas = FigureCanvasAgg(fig)mu, sigma = 100, 15x = mu + sigma*randn(10000)# the histogram of the datan, bins, patches = ax.hist(x, 50, normed=1)# add a 'best fit' liney = normpdf( bins, mu, sigma)line, = ax.plot(bins, y, 'r--')line.set_linewidth(1)ax.set_xlabel('Smarts')ax.set_ylabel('Probability')ax.set_title(r'$\rm{Histogram of IQ: }\mu=100, \sigma=15$')ax.set_xlim( (40, 160))ax.set_ylim( (0, 0.03))canvas.draw()s = canvas.tostring_rgb() # save this and convert to bitmap as needed# get the figure dimensions for creating bitmaps or numeric arrays,# etc.l,b,w,h = fig.bbox.get_bounds()w, h = int(w), int(h)if 0: # convert to a Numeric array X = fromstring(s, UInt8) X.shape = h, w, 3if 0: # pass off to PIL import Image im = Image.fromstring( "RGB", (w,h), s) im.show()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -