如何在 matplotlib 2.0 中僅使用陰影填充區域(無背景顏色)?


要在 matplotlib 中使用僅陰影(無背景顏色)填充一個區域,我們可以採取以下步驟 -

步驟

  • 設定圖形大小並調整子圖之間和周圍的邊距。

  • 初始化變數 n 以儲存樣本資料的數量。

  • 建立一個圖形和一組子圖。

  • 繪製 xy 資料點。

  • 使用圓形陰影填充 xy 之間的區域,edgecolor="blue"

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

示例

import numpy as np
import matplotlib.pyplot as plt

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

# Number of sample data
n = 256

# x and y data points
x = np.linspace(-np.pi, np.pi, n, endpoint=True)
y = np.sin(2 * x)

# Figure and set of subplots
fig, ax = plt.subplots()

# Plot the data points
ax.plot(x, y, color='blue', alpha=1.0)

# Fill the area between the data points
ax.fill_between(x, y, color='blue', alpha=.2, facecolor="none", hatch="o", edgecolor="blue", linewidth=1.0)

# Display the plot
plt.show()

輸出

它將產生以下輸出 -

更新於: 2022 年 2 月 1 日

1 千+ 次瀏覽

開啟您的職業生涯

完成本課程後獲得認證

開始學習
廣告
© . All rights reserved.