文章作者: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é)果圖