當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.axes.Axes.fill_between()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。

軸類包含大多數圖形元素:Axis,Tick,Line2D,Text,Polygon等,並設置坐標係。 Axes實例通過callbacks屬性支持回調。

matplotlib.axes.Axes.fill_between()函數

matplotlib庫的axiss模塊中的Axes.fill_between()函數用於填充兩條水平曲線之間的區域。



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

參數:此方法接受以下描述的參數:

  • x:該參數包含用於定義曲線的數據點的水平坐標。
  • y1:此參數包含用於定義第一條曲線的數據點的y坐標
  • y2:該參數包含用於定義第二條曲線的數據點的y坐標。它是可選的,默認值為0。
  • where:此參數是可選參數。它用於排除某些水平區域的填充。
  • interpolate:此參數也是可選參數。它是錯誤欄行的線寬,默認值為NONE。
  • step:此參數也是可選參數。它用於定義填充是否應為階躍函數。

返回值:這將返回包含繪製的多邊形的PolyCollection。

以下示例說明了matplotlib.axes中的matplotlib.axes.Axes.fill_between()函數:

示例1:

# Implementation of matplotlib function 
      
import numpy as np 
import matplotlib.pyplot as plt 
  
x = np.arange(-5, 5, 0.01) 
y1 = -3 * x*x + 10 * x + 10
y2 = 3 * x*x + x 
  
fig, ax = plt.subplots() 
ax.plot(x, y1, x, y2, color ='black') 
ax.fill_between(x, y1, y2, where = y2 >y1,  
                facecolor ='green', alpha = 0.8) 
  
ax.fill_between(x, y1, y2, where = y2 <= y1, 
                facecolor ='black', alpha = 0.8) 
  
ax.set_title('matplotlib.axes.Axes.fill_between Example1') 
plt.show()

輸出:

示例2:

# Implementation of matplotlib function 
      
import numpy as np 
import matplotlib.pyplot as plt 
  
x = np.arange(0.0, 2, 0.01) 
y1 = np.sin(2 * np.pi * x) 
y2 = 0.8 * np.sin(4 * np.pi * x) 
  
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, 
                                         sharex = True, 
                                         figsize =(6, 6)) 
  
ax1.fill_between(x, y1, facecolor ='green') 
ax1.set_title('Fill_Between y1 and 0') 
  
ax2.fill_between(x, y1, 1, facecolor ='green') 
ax2.set_title('Fill_Between y1 and 1') 
  
ax3.fill_between(x, y1, y2, facecolor ='green') 
ax3.set_title('Fill_Between y1 and y2') 
ax3.set_xlabel('x') 
fig.tight_layout() 
  
ax4.fill_between(x, y1, y2, where = y2 <= y1, 
                 facecolor ='green') 
  
ax4.set_title('Fill_Between y1 and y2 with condition y2 <= y1 ' ) 
ax4.set_xlabel('x') 
fig.tight_layout() 
  
plt.show()

輸出:




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.axes.Axes.fill_between() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。