matplotlib的基本用法(五)——添加annotation

文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡書

本文主要是關(guān)于matplotlib的一些基本用法。

  • Demo
import matplotlib.pyplot as plt
import numpy as np

# 繪制普通圖像
x = np.linspace(-3, 3, 50)
y = 2 * x + 1

plt.figure()
plt.plot(x, y)

# 將上、右邊框去掉
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 設(shè)置x軸的位置及數(shù)據(jù)在坐標(biāo)軸上的位置
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
# 設(shè)置y軸的位置及數(shù)據(jù)在坐標(biāo)軸上的位置
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 定義(x0, y0)點
x0 = 1
y0 = 2 * x0 + 1

# 繪制(x0, y0)點
plt.scatter(x0, y0, s = 50, color = 'blue')
# 繪制虛線
plt.plot([x0, x0], [y0, 0], 'k--', lw = 2.5)
# 繪制注解一
plt.annotate(r'$2 * x + 1 = %s$' % y0, xy = (x0, y0), xycoords = 'data', xytext = (+30, -30), \
             textcoords = 'offset points', fontsize = 16, arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3, rad = .2'))

# 繪制注解二
plt.text(-3, 3, r'$Test\ text. \mu \sigma_i, \alpha_i$', fontdict = {'size': 16, 'color': 'red'})
plt.show()
  • 結(jié)果
設(shè)置結(jié)果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容