使用 Matplotlib 填充 Python 中曲線和 X 軸之間的區域


為了使用 Matplotlib 填充 Python 中曲線和 X 軸之間的區域,我們可採取以下幾步

步驟

  • 設定圖形尺寸並調整子圖之間以及周圍的邊距。

  • 使用 numpy 建立xy 資料點。

  • 使用plot() 方法繪製xy 資料點。

  • 使用fill_between() 方法填充曲線和 X 軸之間的區域。

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

示例

import matplotlib.pyplot as plt
import numpy as np

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create x and y data points
x = np.linspace(-5, 5, 100)
y = np.sin(x)

# Plot the x and y data points
plt.plot(x, y)

# Fill the region with color
plt.fill_between(x, 0, y, color='orange')

# Display the plot
plt.show()

輸出

將產生以下輸出 −

更新於: 09-Oct-2021

2K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.