網(wǎng)站首頁 編程語言 正文
用法:
Axes.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)
參數(shù)說明:
基礎(chǔ)用法
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()
當然這樣時沒有多大意義的,只是想展示出一個比較明確的填充,類似于區(qū)域全部填充顏色
案例
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()
復(fù)雜的fille_between(案例來源官網(wǎng))
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
定義從何處排除要填充的某些水平區(qū)域。填充區(qū)域由坐標x[其中]定義。更準確地說,如果其中[i]和其中[i+1],則在x[i]和x[i+1]之間填充。請注意,此定義意味著where中兩個假值之間的孤立真值不會導(dǎo)致填充。由于相鄰的假值,真實位置的兩側(cè)仍保持未填充狀態(tài)。
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或類似的詞。默認情況下,定義填充區(qū)域的多邊形節(jié)點將僅放置在x陣列中的位置。這樣的多邊形無法描述上述靠近交點的語義。包含交叉點的x截面僅被剪裁。
將“插值”設(shè)置為True將計算實際交點,并將填充區(qū)域延伸到此點。
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
包含參數(shù)為三個{‘pre’,‘post’,‘mid’}
如果填充應(yīng)為階躍函數(shù),即x之間的常數(shù),則定義階躍。該值確定階躍發(fā)生的位置:
- “pre”:y值從每個x位置持續(xù)向左,即間隔(x[i-1],x[i]]的值為y[i]。
- “post”:y值從每個x位置持續(xù)向右,即區(qū)間[x[i],x[i+1])的值為y[i]。
- “mid”:步數(shù)出現(xiàn)在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()
偏移會有點不一樣,因為函數(shù)的緣故,偏移不太明顯
原文鏈接:https://blog.csdn.net/KIKI_ZSH/article/details/124104576
相關(guān)推薦
- 2021-11-09 C++11?thread多線程編程創(chuàng)建方式_C 語言
- 2022-07-22 SpringBoot允許跨域訪問配置
- 2022-08-23 C++簡明分析inline函數(shù)的使用_C 語言
- 2023-02-12 如何用C++求兩個數(shù)的最大公約數(shù)和最小公倍數(shù)_C 語言
- 2023-11-12 ubuntu18.04 開機后 USB 端口不能使用;臨時添加usb驅(qū)動導(dǎo)致鼠標鍵盤不可用ubunt
- 2023-04-07 關(guān)于vector的常見用法詳解_C 語言
- 2022-11-19 項目適?Oracle改造及SSL安全性配置問題匯總詳解_oracle
- 2022-04-04 程序員新人入職第一天的基本操作(程序員入職第一天干啥)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支