網站首頁 編程語言 正文
用法:
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()
偏移會有點不一樣,因為函數的緣故,偏移不太明顯
原文鏈接:https://blog.csdn.net/KIKI_ZSH/article/details/124104576
相關推薦
- 2023-12-23 Vercel 部署本地項目
- 2022-08-26 Python實現雙向RNN與堆疊的雙向RNN的示例代碼_python
- 2023-01-15 盤點SqlServer?分頁方式和拉姆達表達式分頁_MsSql
- 2022-05-15 C++?Clock類模擬實現鬧鐘運行_C 語言
- 2022-06-02 使用kubeadm命令行工具創建kubernetes集群_云和虛擬化
- 2022-09-13 c++如何實現歸并兩個有序鏈表_C 語言
- 2022-04-02 jquery實現邊框特效_jquery
- 2021-12-07 基于Kubernetes實現前后端應用的金絲雀發布(兩種方案)_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支