听莫凡 matplotlib笔记2
#第十一节课柱状图
##import matplotlib.pyplot as plt
##import numpy as np
##
##n = 12
##X = np.arange(n)
##Y1 = (1-X/float(n))*np.random.uniform(0.5,1.0,n)
##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")
###+,-代表上下,facecolor="#9999ff"代表主题颜色,edgecolor="white"边框颜色
##for x,y in zip(X,Y1):
## plt.text(x,y+0.05,"%.2f" % y,ha="center",va ="bottom")
##
##for x,y in zip(X,Y2):
## plt.text(x,-y-0.05,"-%.2f" % y,ha="center",va ="top")
##
### 加文字,x+0.4,y+0.05 ,代表位置,"%.2f" % y,传进来的y保留两个小数。ha="center",va ="bottom"横向居中对其,纵向底部对其。
##
##plt.xlim(-0.5,n)
##plt.xticks(())
##plt.ylim(-1.25,1.25)
##plt.yticks(())
##
##plt.show()
###第十二节课 Contours等高线 图
##import matplotlib.pyplot as plt
##import numpy as np
##
##def f(x,y):
## #the height function
## 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)
##X,Y = np.meshgrid(x,y)
###np.meshgrid定义网格
##
###use plt.contourf to filling contours
###X,Y and value for(X,Y)point
##plt.contourf(X,Y,f(X,Y),6,alpha=0.75,cmap=plt.cm.hot)
##
###8是代表九条线分成十份,cmap=plt.cm.hot代表不同值的颜色,
##
###use plt.contour to add coutour lines
##C = plt.contour(X,Y,f(X,Y),6,colors="black",linewidths=.5)
###画等高线
##
###adding label
##plt.clabel(C,inline=True,fontsize=10)
###inline=True代表数字在线中间,Flase代表线穿过数字,fontsize=10,字的大小。
##
##
##plt.show()
#莫凡用的是8,但是8画出来的图对不上,改成6刚好对上,原因等后面去学习以下等高线原理。。
#第13节课 image图片略
#第14结课 3D数据图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
#X,Y value
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)
#height value
Z = np.sin(R)
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap("rainbow"))
ax.contour(X,Y,Z,zdir="z",offset=-2,cmap="rainbow")
ax.set_zlim(-2,2)
plt.show()
#第十五节课
import matplotlib.pyplot as plt
##plt.figure()
##
##plt.subplot(2,2,1)
##
###分成两行两列,四部分,1代表第一个图。
##
##plt.plot( [0,1],[0,1])
###plt.plot( [0,1],[0,1])这个地方代表坐标轴的数值。
##plt.subplot(2,2,2)
##plt.plot( [0,1],[0,2])
##
##plt.subplot(2,2,3)
##plt.plot( [0,1],[0,3])
##
##plt.subplot(224)
##plt.plot( [0,1],[0,4])
##
##plt.show()
##import matplotlib.pyplot as plt
##
##plt.figure()
##
##plt.subplot(2,1,1)
##
###分成两行两列,四部分,1代表第一个图。
##
##plt.plot( [0,1],[0,1])
##
##plt.subplot(2,3,4)
##plt.plot( [0,1],[0,2])
##
##plt.subplot(2,3,5)
##plt.plot( [0,1],[0,3])
##
##plt.subplot(236)
##plt.plot( [0,1],[0,4])
##
##plt.show()
#第十六节课
#方法1
##import matplotlib.pyplot as plt
##import matplotlib.gridspec as gridspec
##plt.figure()
##ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)
###(3,3)三行三列,(0,0)起始坐标点,colspan=3三列,rowspan=1,一行。
##ax1.plot([1,2],[1,2])
##ax1.set_title("ax1_title")
##
##ax2 = plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1)
##ax3 = plt.subplot2grid((3,3),(1,2),colspan=2,rowspan=2)
##ax4 = plt.subplot2grid((3,3),(2,0),colspan=1,rowspan=1)
##ax5 = plt.subplot2grid((3,3),(2,1),colspan=1,rowspan=1)
##
##plt.show()
#方法2
##import matplotlib.pyplot as plt
##import matplotlib.gridspec as gridspec
##
##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[-1,0])
##ax5 = plt.subplot(gs[-1,-2])
##
##plt.tight_layout()
###自动调整表格,使其更好的填充画布。
##plt.show()
#方法3
import matplotlib.pyplot as plt
f,((ax11,ax12),(zx21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)
#2,2,两行两列,sharex=True共享x轴,sharey=True共享y轴,flase代表不共享,
#
ax11.scatter([1,2],[1,2])
plt.tight_layout()
plt.show()
#17节课图中图
##import matplotlib.pyplot as plt
##import matplotlib.gridspec as gridspec
##
##fig = plt.figure()
##x = [1,2,3,4,5,6,7]
##y = [1,3,4,2,5,8,6]
##
##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("title")
##
##left,bottom,width,height = 0.2,0.6,0.25,0.25
##ax2 = fig.add_axes([left,bottom,width,height])
##ax2.plot(y,x,'b')
##ax2.set_xlabel("x")
##ax2.set_ylabel("y")
##ax2.set_title("title inside 1")
##
##plt.axes([0.6,0.2,0.25,0.25])
##plt.plot(y[::-1],x,"g")
##plt.xlabel("x")
##plt.ylabel("y")
##plt.title("title inside 2")
#plt.show()
#18节课主次坐标轴
##import matplotlib.pyplot as plt
##import numpy as np
##x = np.arange(0,10,0.1)#起点0终点10步长0.1.0,0.1,..9.9
##y1 = 0.05*x**2
##y2 = -1*y1
##
##fig,ax1 = plt.subplots()
##ax2 = ax1.twinx()#镜像函数
##ax1.plot(x,y1,"g-")
##ax2.plot(x,y2,'b-')
##
##ax1.set_xlabel('Xdata')
##ax1.set_ylabel('Y1',color='g')
##ax2.set_ylabel('Y2',color='b')
##
##
##plt.show()
#第19节课Animation动画
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation
fig,ax = plt.subplots()#在fig上创建一个1×1网格
x = np.arange(0,2*np.pi,0.01) # 2*np.pi代表2Π
line, = ax.plot(x,np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/15))#i/15改变15可以加快图形变化速度。
return line,
#return line, 打逗号原因,返回是列表,用列表第一位。
def init():
line.set_ydata(np.sin(x))
return line,
ani = animation.FuncAnimation(fig=fig,func=animate,frames=100,init_func=init,interval=20,blit=True)
#fig=fig传进去图片,func=animate传进去函数,为了不和animation重复改成animate,
#frames=100,100个数据循环一次,init_func=init图像起始样子,interval=20频率,20ms更新一次,
#blit=True变化更新,不变化不更新,全部更新用false,mac用户用flase.
plt.show()

浙公网安备 33010602011771号