Matplotlib 文本、箭頭的使用方法


很多時候,光是圖像是不足以表達所有的內容的,需要一些說明性的文字來進行輔助。

在Matplotlib中,使用plt.text方法為圖例添加文本。

x = np.linspace(0,10,100) plt.plot(x, np.cos(x)) plt.text(5,0.5,'this is a cos(x) curve',ha='center',va='center')
plt.show()

plt.text方法的簽名如下:

plt.text(x, y, s, fontdict=None, withdash=False, **kwargs)

下面是常用的參數說明:

  • x,y:坐標值,文字放置的位置
  • string:文字內容字符串
  • size:字體大小
  • alpha:設置字體的透明度
  • family: 設置字體
  • style:設置字體的風格
  • wight:字體的粗細
  • verticalalignment:垂直對齊方式,縮寫為va。可用值 ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’
  • horizontalalignment:水平對齊方式 ,縮寫為ha。可用值‘center’ | ‘right’ | ‘left’
  • xycoords:選擇指定的坐標軸系統
  • bbox給標題增加外框 ,常用參數如下:
  • boxstyle:方框外形
  • facecolor:(簡寫fc)背景顏色
  • edgecolor:(簡寫ec)邊框線條顏色
  • edgewidth:邊框線條大小
  • 其它未列出

下面是個彩色的例子:

plt.text(0.3, 0.3, "hello", size=50, rotation=30.,ha="center", va="center",bbox=dict(boxstyle="round",ec=(1, 0.5, 0.5),fc=(1, 0.8, 0.8),)) plt.text(0.8, 0.8, "world", size=50, rotation=-40.,ha="right", va="top",bbox=dict(boxstyle="square",ec=(1, 0.2, 0.5),fc=(1, 0.3, 0.8),))

很多時候,文本是以數學公式出現的:

x = np.linspace(0,10,100) plt.plot(x, np.sin(x)) plt.title(r'$\alpha_i > \beta_i$', fontsize=20) plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20) plt.text(3, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',fontsize=20)

除了文本,簡單的箭頭也是一種很有用的說明性注釋。雖然有一個plt.arrow()方法可以實現箭頭的功能,但是由於它生成的是SVG向量對象,會隨着圖形分辨率的變化而變化,這有可能給我們帶來困擾。所以,我們一般使用plt.annotate()方法來實現箭頭和注釋的功能。下面是其用法演示:

fig, ax = plt.subplots() x = np.linspace(0, 20, 1000) ax.plot(x, np.cos(x)) ax.axis('equal') ax.annotate('local maximum', xy=(6.28, 1), xytext=(10, 4), arrowprops=dict(facecolor='black', shrink=0.05)) ax.annotate('local minimum', xy=(5 * np.pi, -1), xytext=(2, -6), arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=0,angleB=-90"));

annotate方法的簽名如下:

annotate(s='str' ,xy=(x,y) ,xytext=(l1,l2) ,arrowprops=dict(...)..)

主要參數說明:

  • s:注釋文本內容 
  • xy:被注釋對象的坐標位置,實際上就是圖中箭頭的箭鋒位置
  • xytext: 具體注釋文字的坐標位置
  • xycoords:被注釋對象使用的參考坐標系
  • extcoords:注釋文字的偏移量
  • arrowprops:可選,增加注釋箭頭

下面是一些箭頭arrowprops參數的基本配置項:

  • width:箭頭寬度,以點為單位
  • frac:箭頭頭部所占據的比例
  • headwidth:箭頭底部的寬度,以點為單位
  • shrink:移動提示,並使其離注釋點和文本一些距離
  • **kwargs:matplotlib.patches.Polygon的任何鍵,例如facecolor

關於箭頭的繪制方式,請參考下面兩個官方連接:

  • https://matplotlib.org/users/annotations.html#plotting-guide-annotation
  • https://matplotlib.org/examples/pylab_examples/annotation_demo2.html

下面是一些具體例子的展示:

fig = plt.figure(1, figsize=(8, 5)) ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-4, 3)) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = ax.plot(t, s, lw=3) ax.annotate('straight', xy=(0, 1), xycoords='data', xytext=(-50, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->")) ax.annotate('arc3,\nrad 0.2', xy=(0.5, -1), xycoords='data', xytext=(-80, -60), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) ax.annotate('arc,\nangle 50', xy=(1., 1), xycoords='data', xytext=(-90, 50), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc,angleA=0,armA=50,rad=10")) ax.annotate('arc,\narms', xy=(1.5, -1), xycoords='data', xytext=(-80, -60), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc,angleA=0,armA=40,angleB=-90,armB=30,rad=7")) ax.annotate('angle,\nangle 90', xy=(2., 1), xycoords='data', xytext=(-70, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=10")) ax.annotate('angle3,\nangle -90', xy=(2.5, -1), xycoords='data', xytext=(-80, -60), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=0,angleB=-90")) ax.annotate('angle,\nround', xy=(3., 1), xycoords='data', xytext=(-60, 30), textcoords='offset points', bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=10")) ax.annotate('angle,\nround4', xy=(3.5, -1), xycoords='data', xytext=(-70, -80), textcoords='offset points', size=20, bbox=dict(boxstyle="round4,pad=.5", fc="0.8"), arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=-90,rad=10")) ax.annotate('angle,\nshrink', xy=(4., 1), xycoords='data', xytext=(-60, 30), textcoords='offset points', bbox=dict(boxstyle="round", fc="0.8"), arrowprops=dict(arrowstyle="->", shrinkA=0, shrinkB=10, connectionstyle="angle,angleA=0,angleB=90,rad=10"))


免責聲明!

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



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