关于matplotlib的那些事

来源:互联网 发布:神魔布袋戏 知乎 编辑:程序博客网 时间:2024/05/18 00:26

图命名是中文乱码

需要执行如下步骤

1,找到如下目录

D:\Python27\Lib\site-packages\matplotlib\mpl-data
2,打开该目录下的matplotlibrc

3,font.family前的#去掉,然后在font.sans-serif  的DejaVu Sans前面添加Microsoft YaHei,

font.sans-serif     :Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, 

完结----

这个并不算原创,但是也不算转载。是自己在网上各种找资料,有些数据并没有改,因为懒得去构造它们之间的关系,我是数学小白,请见谅哈。

#coding=utf-8'''test1.jpgfisk.txt''''''颜色(color 简写为 c):蓝色'b' 绿色'g'红色'r' 蓝绿色(墨绿色)'c' 红紫色(洋红)'m'黄色'y' 黑色'k' 白色'w'线型(linestyle 简写为 ls):实线'-'虚线'--'虚点线'-.'点线':'点'.' 点型(标记marker):像素','圆形'o'上三角'^'下三角'v'左三角'<'右三角'>'方形's'加号'+' 叉形'x'棱形'D'细棱形'd'三脚架朝下'1'(像'丫')三脚架朝上'2'三脚架朝左'3'三脚架朝右'4'六角形'h'旋转六角形'H'五角形'p'垂直线'|'水平线'_'plot()--折线图 hist()--直方图 xlabe() ylabe() xlim() ylim() title() legend() show()'''import matplotlib.pylab as plimport matplotlib.pyplot as pltimport numpy as np#折线图pl.xlabel(u"横轴")pl.ylabel(u"纵轴")pl.title(u"图像")x=np.linspace(0,10,1000)y1=np.sin(x)y2=np.cos(x*x)line,=pl.plot(x,y1,'r-',label=u"y=sin(x)函数图",linewidth=1)pl.plot(x,y2,'b--',label=u"y=cos(x*x)函数图",linewidth=2)#pl.plot(x,y1,x,y2)pl.axis([0,10,-2,2]) #pl.xlim(0,10) pl.ylim(-2,2)'''ax=pl.gca()ax.spines['right'].set_color('none')'''pl.legend(loc="upper right")#loc-upper bottom right ledt 默认best#handles=[]  plot返回的对象#labels=[]  名字pl.show()#绘制带背景颜色的多子图pl.figure(u"绘制多子图1")pl.sca(pl.subplot(221,axisbg='r'))pl.plot(range(10),[i for i in range(10)],'_-b',label=u"图2的第一个子图")pl.legend()#-------pl.sca(pl.subplot(222,axisbg='r'))pl.plot(range(10),[i**2 for i in range(10)],'H--k',label=u"图2的第二个子图")pl.legend()pl.sca(pl.subplot(223,axisbg='r'))pl.plot(range(10),[i**3 for i in range(10)],'h-.g',label=u"图2的第三个子图")pl.legend()pl.sca(plt.subplot(224,axisbg='r'))pl.plot(range(10),[i**4 for i in  range(10)],'pm-',label=u"图2的第四个子图")pl.legend()pl.savefig('test1.jpg')n=np.array([0,1,2,3,4,5])x=np.linspace(-0.75,1,100)fig,ax=pl.subplots(1,4,figsize=(14,5))ax[0].scatter=ax[0].plot(x,x+0.25*np.random.randn(len(x)),'or')#不可以用plax[0].set_title(u"子图1")ax[0].set_xlabel(u"子图1横轴")ax[0].set_ylabel(u"子图1纵轴")ax[1].step(n,n**2,lw=2)ax[2].bar(n,n**2,align='center',width=0.5,alpha=0.2)ax[3].fill_between(x,x**2,x**3,color='r',alpha=0.4)plt.show()#从文件中读取数据pl.figure(u"操作文件")data=np.loadtxt('fisk.txt')pl.plot(data[:,0],data[:,1],'o-r',label=u"从文件中读取数据")pl.legend()pl.show()#柱状图'''rects1=plt.bar(                      #(x,data) 就是所要画的二维数据        left=x,                      #x 是X坐标轴数据,即每个块的x轴起始位置        height=data,                 #data是Y坐标轴的数据,即每个块的y轴高度        width=[0.1,0.2,0.3],         #每一个块的显示宽度        bottom=[1,2,3],              #每一个块的底部高度        color='y',                   #块的颜色        edgecolor='g',               #块的边界颜色        linewidth=2,                 #块的线条宽度        xerr=1,                      #x轴误差bar        yerr=1,                      #y轴误差bar        ecolor='r',                  #误差bar的颜色        capsize=1,                   #误差bar的线条宽度        orientation='vertical',     #块的方向  (horizontal,vertical)        align="center",              #块的位置 (center, left, right)        hold=None        )'''pl.figure(u"直方图1")data=np.random.randint(1,11,3)x=np.arange(len(data))rects=pl.bar(    left=x,height=data,width=[0.1,0.2,0.3],bottom=[1,2,3],#水平柱状图plt.barh,属性中宽度width变成了高度height    color='y',edgecolor='g',linewidth=1,    xerr=1,yerr=1,ecolor='r',capsize=1,    orientation='vertical',align="center",hold=None)pl.figure(u"直方图2")rects=pl.bar(left=(0.2,1),height=(1,0.5),width=0.1,align="center",yerr=0.000001,color=('r','g'))  #一个对象一种颜色pl.xticks((0.2,1),(u'第一列',u'第二列')) #增加直方图脚注pl.show()#直方图,盒图boxplotpl.figure(u"柱状统计图1")l=100+15*np.random.randn(10000)n,bins,patches=pl.hist(l,50,color='y',normed=1,alpha=0.3)pl.axis([40,160,0,0.03])pl.grid(True)pl.figure(u"柱状统计图2")l=np.random.randn(100)pl.sca(pl.subplot(121))pl.hist(l)pl.sca(pl.subplot(122))pl.boxplot(l)pl.show()#散点图pl.figure(u"散点图")x=np.random.randn(1,1000)y=np.random.randn(1,1000)#T=np.arctan2(x,y) #散点的颜色#pl.scatter(x,y,c=T,s=25,alpha=0.4,marker='+') #s--散点的大小pl.plot(x,y,'o')pl.show()#饼图'''plt.pie(data,                          # 每个饼块的实际数据,如果大于1,会进行归一化,计算percentage        explode=[0.0,0.1,0.2],               # 每个饼块离中心的距离        colors=['y','r','g'],               # 每个饼块的颜色        labels=['women','men','unknown'], # 每个饼块的标签        labeldistance=1.2,                   # 每个饼块标签到中心的距离        autopct='%1.1f%%',                  # 百分比的显示格式        pctdistance=0.4,                     # 百分比到中心的距离        shadow=True,                         # 每个饼块是否显示阴影        startangle=0,                        # 默认从x轴正半轴逆时针起        radius=1.0                           # 饼块的半径        )'''pl.figure(u"饼图")da=np.random.randint(1,11,5)pl.pie(da,explode=[0,0,.2,0,0,])pl.show()#对数坐标图w = np.linspace(0.1, 1000, 1000)p = np.abs(1/(1+0.1j*w)) # 计算低通滤波器的频率响应plt.subplot(221)plt.plot(w, p, linewidth=2)plt.ylim(0,1.5)plt.subplot(222)plt.semilogx(w, p, linewidth=2) #semilogx()绘制的X轴对数坐标系plt.ylim(0,1.5)plt.subplot(223)plt.semilogy(w, p, linewidth=2) #为semilogy()绘制的Y轴对数坐标系plt.ylim(0,1.5)plt.subplot(224)plt.loglog(w, p, linewidth=2) #loglog()绘制的双对数坐标系plt.ylim(0,1.5)plt.show()#极坐标图fig=pl.figure(u"")ax=fig.add_axes([0.0,0.0,.6,.6],polar=True)t=np.linspace(0,2*np.pi,100)ax.plot(t,t,color='y',lw=3)pl.show()#等值图y, x = np.ogrid[-2:2:200j, -3:3:300j]z = x * np.exp( - x**2 - y**2)extent = [np.min(x), np.max(x), np.min(y), np.max(y)]plt.figure(figsize=(10,4))plt.subplot(121)cs = plt.contour(z,10,extent=extent)plt.clabel(cs)plt.subplot(122)plt.contourf(x.reshape(-1),y.reshape(-1),z,20) #带填充效果的等值线。plt.show()#动图--http://www.jb51.net/article/66441.htmx=np.random.rand(10)fig=pl.figure(u"1")ax1=fig.add_subplot(111,axisbg='y')line,=ax1.plot(x)def update(data):    line.set_ydata(data)    return line,def data_gen():    while True:        yield np.random.rand(10)ani=animation.FuncAnimation(fig,update,data_gen,interval=100)pl.show()x=np.random.rand(10)m=[[0.03, 0.86, 0.65, 0.34, 0.34, 0.02, 0.22, 0.74, 0.66, 0.65],  [0.43, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0.55],  [0.66, 0.75, 0.01, 0.94, 0.72, 0.77, 0.20, 0.66, 0.81, 0.52]]fig=pl.figure(u"2")ax2=fig.add_subplot(111,axisbg='c')line,=ax2.plot(x)def updata(data):    line.set_ydata(data)    return line,ani=animation.FuncAnimation(fig,updata,m,interval=200)pl.show()et=time.clock()from mpl_toolkits.mplot3d import Axes3Dfig=plt.figure()ax=Axes3D(fig)x=np.arange(-4,4,0.25)y=np.arange(-4,4,0.25)x,y=np.meshgrid(x,y)r=np.sqrt(x**2+y**2)z=np.sin(r)ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))ax.contourf(x,y,z,zdir='z',offset=-2,cmap='rainbow')ax.set_zlim(-2,2)plt.show()

                               

                  

 


完结:有疑问的可以评论。




原创粉丝点击