Rendering
這是想要的效果:
Notes
記錄幾個功能的做法:
- 函數作圖
- 座標軸移到過原點,上、下邊框去掉
- 標出一個點
- 虛線描出這個點的橫、縱座標所在
- 給這個點打標籤
- 圖片標題
- 保存
Code
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
# 函數圖
x = np.arange(0, 3, 0.01)
y = x ** 2 - 3 * x + 2
plt.plot(x, y)
# 標題
plt.title('x^2 - 3x + 2')
# 標點
plt.scatter([1.5], [-0.25], s=25, c='r') # stroke, colour
# 虛線定位:兩點確定一條直線
plt.plot([0, 1.5], [-0.25, -0.25], c='b', linestyle='--')
plt.plot([1.5, 1.5], [0, -0.25], c='b', linestyle='--')
# 點的標籤(座標中加減的 `0.15` 是顯示位置的偏移,避免擋住點)
plt.text(1.5+0.15, -0.25-0.15, 'minima', ha='center', va='bottom', fontsize=10.5) # horizontal alignment
# 座標軸調位
ax = plt.gca()
# 去掉上、右邊框
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# 移到原點
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
# 顯示、保存
plt.show()
fig.savefig('fun.png')