import matplotlib.pyplot as plt # 注意此处如果导入的模块是import matlpotlib as plt 运行plt.plot()会报错 import numpy as np x np.linspace(-1,50) # linespace(x,y) 产生100个元素的行向量 其中的元素在区间[x,y]中等间距分布 # linespace(x,y,n) 产生x,y之间的等间隔的n个数 如果n 1 返回结果为y y x*2 9 plt.plot(x,y) plt.show()3.利用plt.figure()画多张图
import matplotlib.pyplot as plt import numpy as np x np.linspace(1,4,100) y1 x**2 y2 x*2 2 plt.figure() plt.plot(x,y1) plt.figure(num 3,figsize (10,5)) plt.plot(x,y2) plt.plot(x,y1,color blue ,linewidth 5,linestyle -- ) plt.show()
1 坐标刻度值 plt.xlim() plt.ylim() 2 坐标标签 plt.xlabel() plt.ylabel() 3 修改刻度值 plt.xticks() plt.yticks()
import numpy as np x np.linspace(1,4,100) y1 x**2 y2 x*2 2 plt.figure() plt.plot(x,y1) plt.plot(x,y2) plt.xlim((0,5)) plt.ylim((1,20)) plt.xlabel( x ) plt.ylabel( y ) # 换坐标轴 new_ticks np.linspace(-1,7,10) plt.xticks(new_ticks) plt.yticks([0,8,16],[r $really bad$ ,r $bad alpha$ ,r $good$ ]) plt.show()5.显示x/y轴 设置边框plt.gca() spines
import matplotlib.pyplot as plt import numpy as np x np.linspace(-1,4,100) y1 -1*x*2 y2 x*2 2 plt.figure() plt.plot(x,y1) plt.plot(x,y2) plt.xlim((0,5)) plt.ylim((1,20)) new_ticks np.linspace(-1,7,10) plt.xticks(new_ticks) plt.yticks([-8,8,16],[r $really bad$ ,r $bad alpha$ ,r $good$ ]) # gca get current axis 即边框 ax plt.gca() ax.spines[ right ].set_color( none ) ax.spines[ top ].set_color( none ) ax.xaxis.set_ticks_position( bottom ) ax.yaxis.set_ticks_position( left ) ax.spines[ bottom ].set_position(( data ,0)) ax.spines[ left ].set_position(( data ,0)) plt.xlabel( x ) plt.ylabel( y ) plt.show()6.图标 legend
import matplotlib.pyplot as plt import numpy as np x np.linspace(-1,4,100) y1 -1*x*2 y2 x*2 2 plt.figure() # 注意此处一定要有’,‘ l1, plt.plot(x,y1,label up ) l2, plt.plot(x,y2,color red ,linewidth 1.0,linestyle -- ,label down ) plt.legend(handles [l1,l2,],labels [ aaa , bbb ],loc best ) plt.xlabel( x ) plt.ylabel( y ) plt.show()7.注释信息 plt.annotate() plt.text
import matplotlib.pyplot as plt
import numpy as np
x np.linspace(-1,4,100)
y1 -1*x*2
plt.figure()
plt.plot(x,y1)
# gca get current axis 即边框
ax plt.gca()
ax.spines[ right ].set_color( none )
ax.spines[ top ].set_color( none )
ax.xaxis.set_ticks_position( bottom )
ax.yaxis.set_ticks_position( left )
ax.spines[ bottom ].set_position(( data ,0))
ax.spines[ left ].set_position(( data ,0))
y0 -1*x0*2
# 散点图
plt.scatter(x0,y0,s 50,color b )
plt.plot([x0,x0],[y0,0], k-- ,lw 2.5,color red )
plt.annotate(r $2x 1 %s$ %y0,xy (x0,y0),xycoords data ,xytext ( 30,-30),textcoords offset points ,
fontsize 16,arrowprops dict(arrowstyle - ,connectionstyle arc3,rad .2 ))
plt.xlabel( x )
plt.ylabel( y )
plt.text(1,-6,r $this is the some text. mu sigma_i$ ,
fontdict { size :16, color : r })
plt.show()
8.tick 透明度 了解 画出来不好看
for label in ax.get_xticklabels() ax.get_yticklabels(): label.set_fontsize(12) # alpha参数表示不透明度 label.set_bbox(dict(facecolor gray ,edgecolor None ,alpha 0.7))9.散点图 scatter
import matplotlib.pyplot as plt import numpy as np n 1024 x np.random.normal(0,1,n) # 正态分布 平均值0 方差1 n个 y np.random.normal(0,1,n) t np.arctan2(x,y) # 为了画出来的点颜色好看 # plt.scatter(x,y,s 75,c t,alpha 0.5) # np.arange() 默认值取0 步长取1 参数 plt.scatter(np.arange(5),np.arange(5)) # plt.xlim((-1,2)) # plt.ylim((-1,2)) plt.xticks() plt.yticks() plt.show()10.柱状图 bar
import matplotlib.pyplot as plt import numpy as np x np.arange(n) y1 (1-x/float(n))*np.random.uniform(0.5,1.0,n) #uniform 正态分布 y2 (1-x/float(n))*np.random.uniform(0.5,1.0,n) plt.bar(x, y1,facecolor #9999ff ,edgecolor white ) plt.bar(x,-y2,facecolor #ff9999 ,edgecolor white ) Y1 zip(x,y1) Y2 zip(x,y2) plt.show()11.等高线图 contour
import matplotlib.pyplot as plt import numpy as np def f(x,y): return (1-x/2 x**5 y**3)*np.exp(-x**2-y**2) n 256 x np.linspace(-3,3,n) y np.linspace(-3,3,n) # numpy.meshgrid()——生成网格点坐标矩阵 X,Y np.meshgrid(x,y) # 8表示分成10段 0表示2部分 # contour() 是绘制轮廓线,contourf()会填充轮廓 plt.contourf(X,Y,f(X,Y),8,alpha 0.75,cmap plt.cm.hot) C plt.contour(X,Y,f(X,Y),8,colors black ,linewidth 0.5) plt.clabel(C,inline True,fontsize 10) plt.xticks(()) plt.yticks(()) plt.show()12.画色块图 plt.imshow() plt. colorbar()
import matplotlib.pyplot as plt import numpy as np import random a np.random.rand(9) b np.array(a).reshape(3,3) print(a) plt.xticks() plt.yticks() # nearest清晰 plt.imshow(b,interpolation nearest ,cmap bone ,origin upper ) plt.colorbar(shrink 0.9) plt.show()13.3D图和等高线图 Axes3D
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig 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) # rstride 跨度 ctride ax.plot_surface(x,y,z,rstride 2,cstride 2,cmap rainbow ,edgecolor black ) ax.contourf(x,y,z,zdir z ,offset -2,cmap rainbow ) ax.set_zlim(-2,2) plt.show()14.大的figure画多张小图 subplot
import matplotlib.pyplot as plt plt.figure() # plt.subplot(2,2,1) # 2行2列第一个位置 下标是从1开始的 plt.subplot(2,1,1) plt.plot([0,1],[0,1]) plt.subplot(2,3,4) # 划分多少个图 2x3 是第几个图 plt.plot([0,1],[2,3]) plt.subplot(2,3,5) plt.plot([0,1],[2,3]) plt.subplot(2,3,6) plt.plot([0,1],[2,3]) plt.show()15.使用grid分格显示
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec # method1: 坐标是从0开始的 plt.figure() ax1 plt.subplot2grid((3,3),(0,0),colspan 3,rowspan 1) ax1.plot([0,1],[2,5]) ax1.set_title( subplot1 ) # 不是figure所以用的set_title ax2 plt.subplot2grid((3,3),(1,0),colspan 2,rowspan 1) ax3 plt.subplot2grid((3,3),(1,2),colspan 1,rowspan 2) ax4 plt.subplot2grid((3,3),(2,0),colspan 1,rowspan 1) ax5 plt.subplot2grid((3,3),(2,1),colspan 1,rowspan 1) # method2: plt.figure() gs gridspec.GridSpec(3,3) #通过索引的方式 ax1 plt.subplot(gs[0,:]) ax2 plt.subplot(gs[1,:2]) ax3 plt.subplot(gs[1:,2]) ax4 plt.subplot(gs[2,0]) ax5 plt.subplot(gs[2,1]) # method3: plt.figure() f,((ax11,ax12),(ax21,ax22)) plt.subplots(2,2,sharex True,sharey True) plt.show()16.图中图 暂时有问题
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig plt.figure() x [1,2,3,4,5,6,7] y [1,2,5,7,5,6,9] left,bottom,width,height 0.1,0.1,0.8,0.8 ax1 fig.add_axes([left,bottom,width,height]) ax1.plot(x,y, r ) ax1.set_xlabel( x ) ax1.set_ylabel( y ) ax1.set_title( this is title ) plt.axes([ 0.1,0.1,0.8,0.8]) plt.plot(y[::-1],x, g ) plt.xlabel( x ) plt.ylabel( y ) plt.title( sha ) plt.show()
还有两个视频没有看完 后续再看吧~现在感觉用不到
学习资源: 【莫烦Python】Matplotlib Python 画图教程.



