我對 fill_between 有問題。當我繪制 2 條不可見線并在它們之間填充時,總會有一條細線可見,盡管此時的值是相同的。所以我希望在那里什么也看不到(見圖)。如您所見,有一條紅色細線可見,但我可以告訴您,這兩個值完全相同。有任何想法嗎?
謝謝!

重現代碼:
import matplotlib.pyplot as plt
y1 = [10, 18, 17, 19, 11, 13, 15, 15, 13, 15, 10, 19, 18, 10, 16, 12, 12, 13, 10, 10]
y2 = [22, 29, 30, 19, 11, 13, 15, 29, 30, 31, 22, 37, 31, 26, 28, 23, 25, 23, 27, 28]
plt.fill_between(range(20), y1, color='blue')
plt.fill_between(range(20), y1, y2, color='red')
uj5u.com熱心網友回復:
您可以將線寬更改為 0,或將邊緣顏色設定為edgecolor='none'。或者您可以為兩個呼叫使用共同的邊緣顏色。
import matplotlib.pyplot as plt
import numpy as np
# create some dummy test data
x = np.linspace(0, 1, 20)
y1 = np.random.rand(20) 1
y2 = np.random.rand(20) 1
y2[10:15] = 0 # fill a part with zeros
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4), sharex=True, sharey=True)
ax1.fill_between(x, 0, y1, color='skyblue')
ax1.fill_between(x, y1, y1 y2, color='tomato')
ax1.set_title('default fill_between')
ax2.fill_between(x, 0, y1, color='skyblue', linewidth=0)
ax2.fill_between(x, y1, y1 y2, color='tomato', linewidth=0)
ax2.set_title('linewidth=0')
ax3.fill_between(x, 0, y1, color='skyblue')
ax3.fill_between(x, y1, y1 y2, color='tomato', edgecolor='black')
ax3.set_title("edgecolor='black'")
ax1.margins(x=0, y=0)
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/362637.html
標籤:Python matplotlib
