如何在 Matplotlib 中先繪製線條,最後繪製點?


若要先繪製線條,最後繪製點,我們可以採取以下步驟 -

  • 使用 numpy 建立 x 點、y1 點和 y2 點,繪製線條。

  • 使用 plot() 方法和 x、y1 和 y2 點繪製曲線。

  • 使用 scatter 方法繪製散點。

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

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
xpoints = np.linspace(1, 1.5, 10)
y1points = np.log(xpoints)
y2points = np.exp(xpoints)
plt.plot(xpoints, y1points)
plt.plot(xpoints, y2points)
for i in xpoints:
   plt.scatter(i, np.random.randint(10))
plt.show()

輸出

更新於: 10-Apr-2021

443 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.