pylab 包含matplotlib,numpy等,引入了pylab就不需要分别引入matplotlib, numpy了。
from pylab import *
t = arange(0.0,2.0,0.01)
s = sin(2*pi*t)
plot(t,s) # x, y axis
xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True) #有小方格
savefig("test.png")
show()
在一张图里画几张子图
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linespace(0.0,5.0)
x2 = np.linespace(0.0,2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
ply.subplot(2,1,1) #2行1列,这个语句画的是第一个图
plt.plot(x1,y1,'yo-')
# 第3个参数是LineSpec,y:黄色, o:空心圆, -:实线
plt.title('A tale of 2 wubplots')
plt.ylabel('Damped oscillation')
plt.subplot(2,1,2)
plt.plot(x2,y2,'r.-')
# r:red . 实心圆 - 实线
plt.xlavel('time (s)')
plt.ylabel('Undamped')
matplotlib.pyplot.hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False,
bottom=None, histtype=u'bar', align=u'mid', orientation=u'vertical', rwidth=None, log=False,
color=None, label=None, stacked=False, hold=None, **kwargs)
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
# example data
mu = 100
sigma = 15
x = mu + sigma * np.random.randn(10000)
num_bins = 50
# the histogram of the data
n,bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
#add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
# Tweak spacing to prevent clipping of ylabel
# 调整间距防止y轴重叠
plt.subplots_adjust(left=0.15)
plt.show()