使用matplotlib画简单的图形:
#-*- coding:utf-8 -*- from numpy.random import randn import matplotlib.pyplot as plt fig=plt.figure() ax1=fig.add_subplot(2,2,1) plt.plot(randn(50).cumsum(),'k--') ax2=fig.add_subplot(2,2,2) #bins越大矩形越窄 alpha表示颜色深度 ax2.hist(randn(10000), bins = 30, color = 'red', alpha = 1) ax3=fig.add_subplot(2,2,3) plt.plot([1.5, 2, 4, -2, 1.6]) plt.show()
运行结果:
散点图
#-*- coding:utf-8 -*- from pylab import * import matplotlib.pyplot as plt mpl.rcParams['font.sans-serif'] = ['SimHei'] n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) for i in range(1,10): scatter(i, i) plt.title(u"散点图",color='red') show()
pyplot.subplots有几个选项 nrows:subplot的行数 ncols:subplot的列数 sharex:所有subplot共享x轴刻度 sharey:所有subplot共享Y轴刻度
#-*- coding:utf-8 -*- from numpy.random import randn from matplotlib import pyplot as plt fig,axes=plt.subplots(2,2,sharex=True,sharey=True) for i in range(2): for j in range(2): axes[i,j].hist(randn(50),bins=50,color='red',alpha=1) plt.show()
矩阵图
#-*- coding:utf-8 -*- from pylab import * #使用中文 mpl.rcParams['font.sans-serif'] = ['SimHei'] #显示负号 matplotlib.rcParams['axes.unicode_minus'] = False n=32 list1=[i for i in range(1,33)] list2=[i for i in range(-32,0)] n= np.arange(n) xlim(-1,32) ylim(-35,35) xlabel(u'每个城市招聘人数'