赞
踩
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,1,50)#-1到1 有五十个点
y = 2*x+1
plt.figure(num=1,figsize=(3.5, 3.5), dpi=200)
plt.plot(x,y)
plt.savefig('折线图.png')
plt.show()
注意!
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-3,3,50)
y2 = x**2
plt.plot(x,y1)
plt.plot(x,y2,color = "red",linewidth=4.0,linestyle="--")
import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure(num=4,figsize=(4, 4), dpi=200) #相当于放大器,看某个地方 plt.xlim((-1,2)) plt.ylim((-2,3)) #横坐标正坐标名称 plt.xlabel("I am x") plt.ylabel("I am y") #更改横坐标的起始位置和大小 new_ticks = np.linspace(-1,2,5) plt.xticks(new_ticks) #基本操作 #plt.yticks([-2,-1.8,-1.22,3] # ,["really bad" , "bad" , "normal","good"]) #正则表达式,换字体 #plt.yticks([-2,-1.8,-1.22,3] # ,[r"$really\ bad$" , r"$bad$" , r"$normal$",r"$good$"]) #特殊符号 #这里的引入了alpha希腊符号,注意这里要\加空格再\ plt.yticks([-2,-1.8,-1.22,3] ,[r"$really\ bad$" , r"$bad\ \alpha$" , r"$normal$",r"$good$"]) plt.plot(x,y1) plt.plot(x,y2,color = "red",linewidth=2.0,linestyle="--") plt.show()
import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure(num=4,figsize=(4, 4), dpi=200) #相当于放大器,看某个地方 plt.xlim((-1,2)) plt.ylim((-2,3)) #横坐标正坐标名称 plt.xlabel("I am x") plt.ylabel("I am y") #更改横坐标的起始位置和大小 new_ticks = np.linspace(-1,2,5) plt.xticks(new_ticks) plt.yticks([-2,-1.8,-1.22,3] ,[r"$really\ bad$" , r"$bad$" , r"$normal$",r"$good$"]) ax = plt.gca() #spines 脊梁 #设置边框 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.plot(x,y1) plt.plot(x,y2,color = "red",linewidth=2.0,linestyle="--") plt.show()
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure(num=4,figsize=(4, 4), dpi=200) plt.xlim((-1,2)) plt.ylim((-2,3)) plt.xlabel("I am x") plt.ylabel("I am y") new_ticks = np.linspace(-1,2,5) plt.xticks(new_ticks) plt.yticks([-2,-1.8,-1.22,3] ,[r"$really\ bad$" , r"$bad$" , r"$normal$",r"$good$"]) #label给线命名 #如果想传到handle里面去,l1 l2 后面要加逗号 #逗号叫做拆包,序列拆包,返回超过第一个 l1, = plt.plot(x,y1,label='up') l2, = plt.plot(x,y2,color = "red",linewidth=2.0,linestyle="--",label='down') #loc='best' 自动找一个最好的地方 还有upper up down plt.legend(handles=[l1,l2],labels=['aaa','bbb'],loc='best') plt.show()
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50) y1 = 2*x+1 plt.figure(num=4,figsize=(4, 4), dpi=200) ax = plt.gca() #spines 脊梁 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.scatter plt.plot(x,y1) x0 = 1 y0 = 2*x0 + 1 #画出一个点 scatter散点图 #s(size)大小 #区别plot,plot画的是线 用的是linewidth plt.scatter(x0,y0,s=10,color='blue') #画出这条虚线 plt.plot([x0,x0],[y0,0],color='black',linestyle="--",linewidth=1.5) # method 1 ############### #xy:标注点的坐标,xycoords:标注点的坐标系,是基于我的data的 # xytext:标记点的位置,textcoord:坐标系(这里是相对坐标系) plt.annotate(r'$2x+1=%s$'%y0, xy=(x0,y0),xycoords = 'data',xytext=(+30,-30), textcoords='offset points',fontsize=10,arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) #method 2 #-5,3是text起始的位置 #r后面是text文本内容,加美元符号是为了改变字体,每一个空格要被识别都要加转置符 #fontdic是对于前面的值的一个参数设置,用键值对来 plt.text(-5,3,r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i \alpha_t$', fontdict={'size':10,'color':"red"}) plt.show()
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50) y = 0.1*x plt.figure(num=5,figsize=(4, 4), dpi=200) #zorder设置图层顺序,越大越在上面 plt.plot(x,y,linewidth=10,zorder=1) plt.ylim(-2,2) ax = plt.gca() #设置xy坐标轴,把非坐标轴的框框删掉 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)) #每一个label(标签)都拿出来改 for label in ax.get_xticklabels() + ax.get_yticklabels(): #设置label的大小 label.set_fontsize(6) #zorder设置图层顺序,越大越在上面,这里的bbox在line的上面 label.set_zorder(2) #facelolor设置标签底色,edgecolor设置边框颜色,alpha是透明度 label.set_bbox(dict(facecolor='blue',edgecolor="None",alpha=0.05)) plt.show()
import matplotlib.pyplot as plt import numpy as np #创建一个figure plt.figure(num=4,figsize=(6, 5), dpi=200) n = 1024 #正态随机数,在这里,正负越靠近0的数出现的概率就越高 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) T = np.arctan2(Y,X)#for color value plt.xticks(()) plt.yticks(()) plt.xlim((-1.5,1.5)) plt.ylim((-1.5,1.5)) plt.scatter(X,Y,s=40,c=T,alpha=0.5) plt.savefig('散点图.jpg') plt.show()
import matplotlib.pyplot as plt
import numpy as np
#创建一个figure
plt.figure(num=4,figsize=(6, 5), dpi=200)
n = 1024
#正态随机数,在这里,正负越靠近0的数出现的概率就越高
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
T = np.arctan2(Y,X)#for color value
plt.scatter(np.arange(5),np.arange(5),c="red")
plt.show()
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.figure(num=1,figsize=(4,3),dpi=200) plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white') plt.bar(X,-Y2,facecolor='#ff9999',edgecolor="white") #zip把X和Y1打包成元组 #把X和Y传到和中,如果没有zip的话只会传一个值 for x,y in zip(X,Y1): #ha horizontal alignment plt.text(x,y+0.05,r'%.2f'%y,ha='center',va='bottom',fontsize=7) for x,y in zip(X,-Y2): #ha horizontal alignment plt.text(x,y-0.05,r'%.2f'%y,ha='center',va='top',fontsize=7) #先限制范围,再关闭显示 plt.xlim(-0.5,n) plt.xticks(()) plt.ylim(-1.25,1.25) plt.yticks(()) plt.show()
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) #创建 plt.figure(num=5,figsize=(4,3),dpi=200) # n = 256 x = np.linspace(-3,3,n) y = np.linspace(-3,3,n) #把X,Y画出网格线 X,Y = np.meshgrid(x,y) #contour等高线 ,后面的f代表filling填充 #0是一条线,分成两部分 8是9条线 分成10部分 #这里的cool可以换成hot变成热力图 plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.cool) C = plt.contour(X,Y,f(X,Y),8,colors='black',linewidths=0.5) #加等高线标注 plt.clabel(C,inline=True,fontsize=6) plt.xticks(()) plt.yticks(()) plt.savefig('等高线.jpg') plt.show()
import matplotlib.pyplot as plt import numpy as np #image data #每一个数都是一个像素点 plt.figure(num=5,figsize=(4,3),dpi=200) a = np.array([0.313660827978, 0.365348418405 , 0.423733120134, 0.365348418405, 0.439599930621, 0.525083754405, 0.423733120134,0.525083754405,0.651536351379]).reshape(3,3) """ interpolation代表空白处插值方式 应该是利用不同的模糊算法,产生一种艺术效果吧。 """ plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower') #调色板,shrink是压缩到原来的百分之几 plt.colorbar(shrink=0.9) plt.xticks(()) plt.yticks(()) plt.show()
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure(figsize=(8,8),dpi=200) 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 行跨 cstride 列跨 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.savefig("3DMap.jpg") plt.show()
import matplotlib.pyplot as plt plt.figure(num=1,figsize=(8,8),dpi=200) #两行两列,在第一张图 plt.subplot(2,2,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(2,2,4) plt.plot([0,1],[0,4])
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec #plt.subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs) #shape就是尺寸 ,location就是你画的这个图从哪个位置开始,colspan列跨度3 ,rowspan行跨度 #注意:location从(0,0)开始 #grid格子 plt.figure() ax1 = plt.subplot2grid((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=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) plt.xlim() plt.tight_layout() plt.show()
#method 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]) #tight_layout会调整子图之间的间隔来减少堆叠,可以自己注释掉看看有什么不同 plt.tight_layout() plt.show()
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4),dpi=200) x = [1,2,3,4,5,6,7] y = [1,3,4,2,5,8,6] #fig.add_axes(*args, **kwargs) 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(x,y,'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,'green') # 这里y[::-1]表示从后往前,间隔为1 plt.xlabel('x') plt.ylabel('y') plt.title('title inside2') plt.show()
import matplotlib.pyplot as plt import numpy as np x = np.arange(0,10,0.1) y1 = 0.05*x**2 y2 = -1*y1 fig,ax1 = plt.subplots() #twinx:共享x轴,twiny:共享y轴 ax2 = ax1.twinx() ax1.plot(x,y1,"g-") ax2.plot(x,y2,"b--") ax1.set_xlabel('X data') ax1.set_ylabel("Y1",color='g') ax2.set_ylabel('Y2',color='b')
import numpy as np from matplotlib import pyplot as plt from matplotlib import animation fig,ax = plt.subplots() x = np.arange(0,2*np.pi,0.01) line, = ax.plot(x,np.sin(x)) def animate(i): line.set_ydata(np.sin(x+i/100)) 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) plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。