1.改变x轴或是y轴的值(刻度):
plt.figure(num=3)
plt.plot(a,b2)
plt.plot(a,b,linewidth=21,linestyle='--')
plt.xlim((-1,2)) # x轴大小范围
plt.ylim((-3,3)) # y 轴大小范围
plt.xlabel("I am x ")
plt.ylabel("I am y")
new_ticks= np.linspace(-1,2,5) #定义一个分隔
plt.xticks(new_ticks) #把这个分隔给x轴
plt.yticks([-2,-1,0,1,2,3,],[
'$too\ bad$','$bad$','$indifferet$','$well$','$good$','$very\ good$'
]) #把y轴分隔点置换为数字。 第一个list代表你要换的点。
plt.show()
结果:
2.移动坐标轴的位置
__author__ = "Luke Liu"
import matplotlib.pyplot as plt
import numpy as np
a=np.linspace(-1,1,50)
b=np.exp(a)
new_ticks= np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-3,-1,0,1,2,3,],[
'$too\ bad$','$bad$','$indifferet$','$well$','$good$','$very\ good$'
])
plt.plot(a,b)
# gca = 'get current axis'
ax=plt.gca()
#去掉上右边框
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#规定 左下 为 y轴与x轴
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#移动y,x
ax.spines['bottom'].set_position(('data',1)) #将x轴移动都y=1处
ax.spines['left'].set_position(('data',-0.5)) #将y轴移动到x=-0.5处
plt.show()
3.在曲线中添加注解
可以用annoate或者text
__author__ = "Luke Liu"
import matplotlib.pyplot as plt
import numpy as np
a=np.linspace(-1,1,50)
b=np.exp(a)
new_ticks= np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-3,-1,0,1,2,3,],[
'$too\ bad$','$bad$','$indifferet$','$well$','$good$','$very\ good$'
])
plt.plot(a,b)
# gca = 'get current axis'
ax=plt.gca()
#去掉上右边框
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#规定 左下 为 y轴与x轴
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#移动y,x
ax.spines['bottom'].set_position(('data',1)) #将x轴移动都y=1处
ax.spines['left'].set_position(('data',0)) #将y轴移动到x=0处
print('________________________________________________________________________')
#标注点
x0=0.8
y0=np.exp(x0)
plt.scatter(x0,y0,s=200,c='r')
plt.plot([x0,x0],[y0,1],'k--',lw=2)
#给标注
plt.annotate(r'$y=exp(0.8)is %s$'%y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',
fontsize=16, arrowprops=dict(arrowstyle='->'))
#或者用test
plt.text(0,0,'oo')
plt.show()
3.散点图scatter
__author__ = "Luke Liu"
import matplotlib.pyplot as plt
import numpy as np
n=1000
x= np.random.normal(0,1,n)
y= np.random.normal(0,1,n)
T=np.arctan2(x,y)
plt.scatter(x,y,c=T,s=100,alpha=0.5)
plt.xlim(-1.5,1.5)
plt.xlim(-1.5,1.5)
#去掉x,y 刻度
plt.xticks(())
plt.yticks(())
plt.show()
4.柱状图Bar
__author__ = "Luke Liu"
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='r',edgecolor='white')
plt.bar(x,-y2,facecolor='y',edgecolor='white')
#标注数值
for x,y in zip(x,y1):
plt.text(x+0.1,y+0.01,"{}".format(round(y,2)),ha='center',va='bottom')
plt.plot(y1,'k--',lw=2)
plt.show()
5.画等高线 contours
__author__ = "Luke Liu"
import matplotlib.pyplot as plt
import numpy as np
#定义一个function
def function(x,y):
return (1-x/2+x**6+y**3)*np.exp(-x**2-y**2)
n=256
x=np.linspace(-3,3,n)
y=np.linspace(-3,3,n)
plt.figure(figsize=(10,6))
#将x,y 网格化
X,Y=np.meshgrid(x,y)
#填充颜色 ,数字表示分为几部分
plt.contourf(X,Y,function(X,Y),8,alpha=0.6,cmap=plt.cm.hot)
#画出等高线,数字表示分为几部分
C= plt.contour(X,Y,function(X,Y),8,colors='black',linewidth=0.5)
#画出标签, inline 表示画在线里面
plt.clabel(C,inline=True,fontsize=10)
plt.show()
6.显示图片
__author__ = "Luke Liu"
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import cv2
filep = r"C:\Users\asus\Desktop\OpenCV&ML in Python\img_0042.png"
img = Image.open(filep)
plt.imshow(img,interpolation='nearest',cmap='bone')
plt.colorbar(shrink=0.8)
plt.axis("off")
plt.show()
7.画3d图像
__author__ = "Luke Liu"
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
#建立3维度坐标系
fig = plt.figure()
ax = Axes3D(fig)
# set X and Y's Value
X = np.arange(-4,4,0.25)
Y = np.arange(-4,4,0.25)
#mesh底面
XX,YY=np.meshgrid(X,Y)
#对应点的高度
R = np.sqrt(XX**2+YY**2)
Z = np.sin(R)
# rstride 与 cstride 显示密集程度
ax.plot_surface(XX,YY,Z,rstride=1,cstride=1,cmap=plt.get_cmap("rainbow"))
#可以画出等高线图,zdir表示将其映射到z上
ax.contour(XX,YY,Z,zdir='z',offset=-2,cmap='rainbow')
plt.show()
8. subplot 分割figure
(1)方法一:
import matplotlib.pyplot as plt
import numpy as np
import cv2
import PIL
x = np.linspace(-1,1,100)
y1 = np.exp(x)
y2 = np.exp(x**3)
y3 = np.exp(x**2)
y4 = np.exp(x*-1)
plt.figure()
plt.subplot(221)
plt.plot(x,y1)
plt.subplot(222)
plt.plot(x,y2)
plt.subplot(223)
plt.plot(x,y3)
plt.subplot(224)
plt.plot(x,y4)
plt.show()
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("BIG AXIS")
10、双坐标系
import matplotlib.pyplot as plt
import numpy as np
import cv2
import PIL
import matplotlib.gridspec as gridspec
x = np.arange(0,10,0.1)
y1 = 0.05 *x**2
y2 = -1 * y1
fig,ax1=plt.subplots()
# 将ax1逆转
ax2=ax1.twinx()
ax1.plot(x,y1,'g-')
ax2.plot(x,y2,'b-')
plt.show()