如何在另一個 Python 圖形中新增不同的圖形(作為插圖)?


要在另一個 Python 圖形中新增不同的圖形(作為插圖),我們可以採取以下步驟 −

  • 使用 numpy 建立 資料點。

  • 使用 subplots() 方法,建立一個圖形和一組子圖,即,fig ax

  • 要建立新軸,請將 axis 新增到現有圖形(步驟 2)。

  • 在軸上繪製 (步驟 2)。

  • 在新軸上繪製 (步驟 3)。

  • 要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-1, 1, 100)
y = np.sin(x)
fig, ax = plt.subplots()
left, bottom, width, height = [.30, 0.6, 0.2, 0.25]
ax_new = fig.add_axes([left, bottom, width, height])
ax.plot(x, y, color='red')
ax_new.plot(x, y, color='green')
plt.show()

輸出

更新於: 06-May-2021

1K+ 瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.