我想在我的散點圖上的兩條線之間填充。這是爲了顯示散點圖平均值附近的標準偏差。fill_between只填充兩個x值,不填充整個散點圖,matplotlib
import matplotlib.pyplot as plt
import numpy as np
y1 # is the first set of y-axis data
y2 # is the second set of y-axis data
y3 # is the third set of y-axis data
x # is the x-axis data set
ym1 = np.mean(y1)
ym2 = np.mean(y2)
ym3 = np.mean(y3)
# To set up the means of the three data series
sigma1 = np.std(y1)
sigma2 = np.std(y2)
sigma3 = np.std(y3)
# To set up the standard deviations
ysupper1 = ym1 + sigma1
ysupper2 = ym2 + sigma2
ysupper3 = ym3 + sigma3
yslower1 = ym1 - sigma1
yslower2 = ym2 - sigma2
yslower3 = ym3 - sigma3
# To set up the higher and lower limits to fill between
yma1 = np.array([ym1])
yma2 = np.array([ym2])
yma3 = np.array([ym3])
# To set up values to put into a plt.axhline
設置完這些之後,我創建了一個針對x的y1,y2和y3圖形,這裏沒有問題發生。然後,我設置了plt.axhline
的和plt.fill_between
的:
plt.axhline(yma1, linestyle='--', color='b', alpha=0.7),
plt.axhline(yma2, linestyle='--', color='r', alpha=0.7),
plt.axhline(yma3, linestyle='--', color='c', alpha=0.7),
# To create the 'mean value' lines on the graph
plt.fill_between(x, ysupper1, yslower1, color='b', alpha=0.3),
plt.fill_between(x, ysupper2, yslower2, color='r', alpha=0.3),
plt.fill_between(x, ysupper3, yslower3, color='c', alpha=0.3),
# To fill in between the upper and lower limits of the standard deviation
然而,這總是創建圖形只有沿x軸的兩個點之間的填充。我試過手動說參數plt.fill_between
爲None
,因爲我預料沒有任何變化。我不知道爲什麼它只能在這兩個點之間沿着x軸看似任意的點。僅供參考這裏是我得到的圖形圖像:
編輯:答案被發現是一如既往,這是因爲不懂得一個功能是如何工作的。
把'fill_between'想象成與'plot'幾乎相同,除了兩行是1,具有相同的'x'-數據。我猜從那裏你可以獨立進步。 –
您的x範圍是您的x軸範圍嗎?我猜你的x只覆蓋了圖的填充部分 – heyu91
從問題的代碼似乎正常工作,所以爲了得到幫助,你應該(一如既往)提供問題的[mcve]。 – ImportanceOfBeingErnest