Annotation 标注
生活随笔
收集整理的這篇文章主要介紹了
Annotation 标注
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、畫出基本圖
當圖線中某些特殊地方需要標注時,我們可以使用annotation. matplotlib 中的annotation有兩種方法, 一種是用 plt 里面的annotate,一種是直接用 plt 里面的text來寫標注.
首先,我們在坐標軸中繪制一條直線.
>>> import matplotlib.pyplot as plt >>> import numpy as np >>> x = np.linspace(-3, 3, 50) >>> y = 2*x + 1 >>> plt.figure(num=1, figsize=(8, 5),) <Figure size 800x500 with 0 Axes> >>> plt.plot(x, y,) [<matplotlib.lines.Line2D object at 0x000001C38F2B4C88>] >>> plt.show()
2、移動坐標
然后我們挪動坐標軸的位置.
>>> ax = plt.gca()
>>> ax.spines['right'].set_color('none')
>>> ax.spines['top'].set_color('none')
>>> ax.spines['top'].set_color('none')
>>> ax.xaxis.set_ticks_position('bottom')
>>> ax.spines['bottom'].set_position(('data', 0))
>>> ax.yaxis.set_ticks_position('left')
>>> ax.spines['left'].set_position(('data', 0))
>>> plt.show()
然后標注出點(x0, y0)的位置信息. 用plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)畫出一條垂直于x軸的虛線.
>>> x0 = 1 >>> y0 = 2*x0 + 1 >>> plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5) [<matplotlib.lines.Line2D object at 0x000001C38E026748>] >>> # set dot styles ... plt.scatter([x0, ], [y0, ], s=50, color='b') <matplotlib.collections.PathCollection object at 0x000001C38E018320> >>> plt.show()
3、添加注釋 annotate
接下來我們就對(x0, y0)這個點進行標注.
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))
plt.show()
其中參數xycoords='data'是說基于數據的值來選位置,xytext=(+30, -30)和textcoords='offset points'對于標注位置的描述 和 xy 偏差值,arrowprops是對圖中箭頭類型的一些設置.
4、添加注釋text
plt.text(-3.7, 3, r'$This is the some text. mu sigma_i alpha_t$',
fontdict={'size': 16, 'color': 'r'})
其中-3.7, 3,是選取text的位置, 空格需要用到轉字符,fontdict設置文本字體.
總結
以上是生活随笔為你收集整理的Annotation 标注的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装VS 2008 EXPRESS出现无
- 下一篇: 大红屏!蓝箭航天抢发SpaceX实现全球