python-annotation與text詳細解釋


import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax1=fig.add_subplot(121)
t=np.arange(0.0,5,0.01)
s=np.cos(2*np.pi*t)
line,=ax1.plot(t,s,lw=2)
bbox=dict(boxstyle='round',fc='white')
ax1.annotate('local max',xy=(2,1),xytext=(3,1.5),
arrowprops=dict(facecolor='black',edgecolor='red',headwidth=7,width=2),bbox=bbox)
#arrowprops里面還有一些參數可選,arrowstyle箭頭類型,arrowstyle="->",connectionstyle="arc3"指的是xy與xytext之間的連接類型
bbox_prop=dict(fc='white')
ax1.set_xlabel('asix-X',bbox=bbox_prop)
#bbox再任何地方都可以使用,包括title里面都是可以使用的,他的意思就是添加方框,但是對方框進行修改的時候要使用字典形式
#設置指示箭頭的形狀是通過字典來調節的,arrowprops字典,headwidth箭頭頭部的寬度,width箭頭身子的寬度
ax1.set_ylim(-2,2)
ax1.text(1,1,'max')
ax2=fig.add_subplot(122)
ax2.set_ylim(-4,4)
ax2.set_xlim(-4,4)
bbox=dict(boxstyle='round',ec='red',fc='white')
ax2.text(-2,0,'$y=sin(x)$',bbox=bbox)
ax2.text(0,-2,'$y=cos(x)$',bbox=dict(boxstyle='square',facecolor='white',ec='black'),rotation=45)
ax2.grid(ls=":",color='gray',alpha=0.5)
#設置水印(帶方框的水印)
ax2.text(-2,2,'matplotlib',fontsize=20,alpha=0.3,color='gray',bbox=dict(fc="white",boxstyle='round',edgecolor='gray',alpha=0.3))
plt.show()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM