python面積圖之曲線圖的填充

用法:

Axes.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)

參數說明:

基礎用法

import matplotlib.pyplot as plt
import numpy as np
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [9, 9, 9, 9, 9]
fig, (ax1, ax2) = plt.subplots(1,2)
ax1.fill_between(x, y1, alpha=.5, linewidth=0)
ax1.set_title('填充x,y1之間')
ax2.fill_between(x, y2, alpha=.5, linewidth=1)
ax2.set_title('填充x,y2之間')
plt.show()

當然這樣時沒有多大意義的,隻是想展示出一個比較明確的填充,類似於區域全部填充顏色

案例

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(1)
x = np.linspace(0, 8, 16)
y1 = 3 + 4*x/8 + np.random.uniform(0.0, 0.5, len(x))
y2 = 1 + 2*x/8 + np.random.uniform(0.0, 0.5, len(x))
fig, ax = plt.subplots()
ax.fill_between(x, y1, y2, alpha=.5, linewidth=0)
ax.plot(x, (y1 + y2)/2, linewidth=2)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()

復雜的fille_between(案例來源官網)

import numpy as np
import matplotlib.pyplot as plt

Nsteps, Nwalkers = 100, 250
t = np.arange(Nsteps)
# an (Nsteps x Nwalkers) array of random walk steps
S1 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
S2 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)
# an (Nsteps x Nwalkers) array of random walker positions
X1 = S1.cumsum(axis=0)
X2 = S2.cumsum(axis=0)
# Nsteps length arrays empirical means and standard deviations of both
# populations over time
mu1 = X1.mean(axis=1)
sigma1 = X1.std(axis=1)
mu2 = X2.mean(axis=1)
sigma2 = X2.std(axis=1)
# plot it!
fig, ax = plt.subplots(1)
ax.plot(t, mu1, lw=2, label='mean population 1')
ax.plot(t, mu2, lw=2, label='mean population 2')
ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='C0', alpha=0.4)
ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='C1', alpha=0.4)
ax.set_title(r'random walkers empirical $\mu$ and $\pm \sigma$ interval')
ax.legend(loc='upper left')
ax.set_xlabel('num steps')
ax.set_ylabel('position')
ax.grid()

where和interpolate

where

定義從何處排除要填充的某些水平區域。填充區域由坐標x[其中]定義。更準確地說,如果其中[i]和其中[i+1],則在x[i]和x[i+1]之間填充。請註意,此定義意味著where中兩個假值之間的孤立真值不會導致填充。由於相鄰的假值,真實位置的兩側仍保持未填充狀態。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = np.arange(0, 4 * np.pi, 0.01)
y = np.sin(x)
ax.plot(x, y, color='black')
ax.fill_between(x, y, 0, where=(x>4)&(x<5),color='cyan', alpha=0.5)
plt.show()

interpolate

在語義上,where通常用於y1>y2或類似的詞。默認情況下,定義填充區域的多邊形節點將僅放置在x陣列中的位置。這樣的多邊形無法描述上述靠近交點的語義。包含交叉點的x截面僅被剪裁。
將“插值”設置為True將計算實際交點,並將填充區域延伸到此點。

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y1 = np.array([0.8, 0.8, 0.2, 0.2])
y2 = np.array([0, 0, 1, 1])

fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.set_title('interpolation=False')
ax1.plot(x, y1, 'o--')
ax1.plot(x, y2, 'o--')
ax1.fill_between(x, y1, y2, where=(y1 > y2), color='C0', alpha=0.3)
ax1.fill_between(x, y1, y2, where=(y1 < y2), color='C1', alpha=0.3)
ax2.set_title('interpolation=True')
ax2.plot(x, y1, 'o--')
ax2.plot(x, y2, 'o--')
ax2.fill_between(x, y1, y2, where=(y1 > y2), color='C0', alpha=0.3,
                 interpolate=True)
ax2.fill_between(x, y1, y2, where=(y1 <= y2), color='C1', alpha=0.3,                interpolate=True)
fig.tight_layout()

step

包含參數為三個{‘pre’,‘post’,‘mid’}
如果填充應為階躍函數,即x之間的常數,則定義階躍。該值確定階躍發生的位置:

  • “pre”:y值從每個x位置持續向左,即間隔(x[i-1],x[i]]的值為y[i]。
  • “post”:y值從每個x位置持續向右,即區間[x[i],x[i+1])的值為y[i]。
  • “mid”:步數出現在x位置的中間。
import numpy as np
import matplotlib.pyplot as plt

a = np.linspace(0,2*3.14,50) 
b = np.sin(a) 
plt.figsize=((12,6))
plt.subplot(131)
plt.fill_between(a, b, 0, where = (a > 2) & (a < 5), color = 'green', step='pre') 
plt.plot(a,b)
plt.title('step=pre')

plt.subplot(132)
plt.fill_between(a, b, 0, where = (a > 2) & (a < 5), color = 'cyan', step='post') 
plt.plot(a,b)
plt.title('step=post')

plt.subplot(133)
plt.fill_between(a, b, 0, where = (a > 2) & (a < 5), color = 'red', step='mid') 
plt.plot(a,b)
plt.title('step=mid')
plt.show()

偏移會有點不一樣,因為函數的緣故,偏移不太明顯

到此這篇關於python面積圖之曲線圖的填充的文章就介紹到這瞭,更多相關python曲線填充內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: