matplotlib4 详解 复杂图形

lesson17两根坐标轴

x= np.arange(2,20,1)

y1 = x*x

y2 = np.log(x)

plt.plot(x,y1)

plt.twinx()

plt.plot(x,y2,color ='r')

plt.show()

matplotlib4 详解 复杂图形

面向对象的做法

x= np.arange(2,20,1)

y1 = x*x

y2 = np.log(x)

fig = plt.figure()

ax1= fig.add_subplot(111)

ax1.plot(x,y1)

ax1.set_ylabel('Y1')

ax2 = ax1.twinx()

ax2.plot(x,y2,'r')

ax2.set_ylabel('Y2')

ax1.set_xlabel('Compare Y1 and Y2')

plt.show(ax1)

matplotlib4 详解 复杂图形


双X轴

x= np.arange(2,20,1)

y1 = x*x

y2 = np.log(x)

fig = plt.figure()

ax1= fig.add_subplot(111)

ax1.plot(y1,x)

ax1.set_xlabel('Y1')

ax2 = ax1.twiny()

ax2.plot(y2,x,'r')

ax2.set_xlabel('Y2')

#ax1.set_xlabel('Compare Y1 and Y2')

plt.show(ax1)

matplotlib4 详解 复杂图形



lesson 18 画标志性箭头说明

 

x = np.arange(-10,11,1)

y = x*x

plt.plot(x,y)

plt.annotate('this is the bottom',xy=(0,1),xytext = (0,20),

arrowprops =dict(facecolor ='r',frac=0.2,headwidth =30, width =20))

plt.show()

matplotlib4 详解 复杂图形


lesson 19 如何画出文字

x = np.arange(-10,11,1)

y = x*x

plt.text(-2,40,"function: y=x*x", family = 'fantasy',size =20, color ='g',style ='italic',

weight =1000, bbox=dict(facecolor='r',alpha= 0.5))

plt.plot(x,y)

plt.show()

matplotlib4 详解 复杂图形



matplotlib4 详解 复杂图形


http://matplotlib.org/users/mathtext.html

lesson 20 数学公式

fig=plt.figure()

ax=fig.add_subplot(111)

ax.set_xlim(1,7)

ax.set_ylim(1,5)

ax.text(2,4,r"$ \alpha_i \beta_j \pi\lambda \omega $", size =15)

ax.text(4,4,r"$ \sina(0) =\cos(\frac{\pi}{2})  $", size =15)

ax.text(2,2,r"$ \lim_{x \rightarrow y}\frac{1} {x^3} $", size =15)

ax.text(4,2,r"$ \sqrt[4]{x} = \sqrt{y}$", size =15)

plt.show()


matplotlib4 详解 复杂图形


lesson 21 弹出框的技巧

N =1000

x = np.random.rand(N)

y = np.random.rand(N)

colors = np.random.rand(N)

area = np.pi*(15*np.random.rand(N)**2)

plt.scatter(x,y, s=area, c=colors,alpha=0.5)

plt.show()

matplotlib4 详解 复杂图形


lesson 22 填充颜色

 

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()

matplotlib4 详解 复杂图形


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 ='k')

#ax.fill_between(x,y1,y2,facecolor ='blue')

ax.fill_between(x,y1,y2, where= y1>=y2,facecolor ='yellow',interpolate = True)

ax.fill_between(x,y1,y2, where= y2>=y1,facecolor ='green',interpolate = True)

plt.show()

matplotlib4 详解 复杂图形