Article Directory
// An highlighted block
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
Graphic annotation
// An highlighted block
x=np.arange(-50,50)
y=x*x
plt.plot(x,y)
#增加注释 ‘’:注释内容,xy:箭头的坐标,xytext:注释内容的起始坐标
#arrowprops:定义形状 facecolor:颜色 frac:箭头和箭身的比例 headwidth:箭头宽度 width:箭身宽度
plt.annotate('min',xy=(0,20),xytext=(0,500),arrowprops=dict(facecolor='r',frac=0.2,headwidth=20,width=10))
plt.show()
Graphical text annotation
// An highlighted block
x=np.arange(-50,50)
y=x*x
plt.plot(x,y)
#添加文字 (-15,500:坐标,('function:y=x*x'):内容 参数:family:字体,size:大小,color:颜色,style:字体斜体,
weight:字体粗细,facecolor:字体加阴影,alpha:透明度
plt.text(-15,500,'function:y=x*x',family='fantasy',
size=20,color='g',style='italic',weight=20,
bbox=dict(facecolor='r',alpha=0.2))
plt.show()
Graphical area filling
Curve area filling
// An highlighted block
x=np.linspace(0,5*np.pi,1000)
y1=np.sin(x)
y2=np.sin(2*x)
plt.plot(x,y1)
plt.plot(x,y2)
Area fill color, alpha: transparency
// An highlighted block
x=np.linspace(0,5*np.pi,1000)
y1=np.sin(x)
y2=np.sin(2*x)
plt.fill(x,y1,'b',alpha=0.3)
plt.fill(x,y2,'r',alpha=0.3)
plt.show()
Area filling between curves
// An highlighted block
x=np.linspace(0,5*np.pi,1000)
y1=np.sin(x)
y2=np.sin(2*x)
fig=plt.figure()
ax=plt.gca()
ax.plot(x,y1,x,y2,color='black')
##当两条线,值大小变化时,填充不同的颜色
ax.fill_between(x,y1,y2,where=y1>=y2,facecolor='y')
ax.fill_between(x,y1,y2,where=y2>y1,facecolor='g')
plt.show()
supplement
tex formula editing
$ As start and end character
// An highlighted block
fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_xlim([1,7])
ax.set_ylim([1,5])
#加公式 r:不转义 官网有公式字母的专门单词,字体大小,下标
ax.text(2,4,r"$ \alpha_i \beta_j \pi \lambda \omega $",
size=25)
ax.text(4,4,r"$ \sin(0)=\cos(\frac{\pi}{2}) $",
size=25)
ax.text(2,2,r"$\lim_{x \rightarrow y} \frac{1}{x^3} $",
size=25)
ax.text(4,2,r"$ \sqrt[4] {x}=\sqrt{y} $",
size=25)
plt.show()
Shape drawing
// An highlighted block
fig, ax=plt.subplots()
#起点坐标
xy1=np.array([0.2,0.2])
xy2=np.array([0.1,0.4])
xy3=np.array([0.5,0.2])
xy4=np.array([0.5,0.45])
#画圆 坐标,半径
circle=mpatches.Circle(xy1,0.1)
ax.add_patch(circle)
#画方块 宽,高
rect=mpatches.Rectangle(xy2,0.2,0.1,color='r')
ax.add_patch(rect)
#画五边形 边数,圆心到顶点的长度
polygon=mpatches.RegularPolygon(xy3,5,0.1,color='black')
ax.add_patch(polygon)
#椭圆 长直径 短直径
ellipsis=mpatches.Ellipse(xy4,0.3,0.1,color='y')
ax.add_patch(ellipsis)
#x,y轴比例协调
plt.axis('equal')
plt.grid()
plt.show()