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


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

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。軸類包含大多數圖形元素:Axis,Tick,Line2D,Text,Polygon等,並設置坐標係。 Axes實例通過callbacks屬性支持回調。

Matplotlib.axes.Axes.bar()函數

matplotlib庫的axiss模塊中的Axes.bar()函數用於製作條形圖。

用法: Axes.bar(self, x, height, width=0.8, bottom=None, *, align=’center’, data=None, **kwargs)


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

  • x:此參數是鋼筋水平坐標的順序。
  • height:此參數是鋼筋的高度。
  • width:此參數是可選參數。它是條的寬度,默認值為0.8。
  • bottom:此參數也是可選參數。它是條形底的y坐標,默認值為0。
  • alighn:此參數也是可選參數。它用於將條形圖與x坐標對齊。

返回值:這將返回以下內容:

  • BarContainer:這將返回帶有所有條和可選的錯誤條的容器。

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

範例1:

# Implementation of matplotlib function 
       
   
import matplotlib.pyplot as plt 
import numpy as np 
  
data = ((30, 1000), (10, 28), (100, 30), 
        (500, 800), (50, 10)) 
  
dim = len(data[0]) 
w = 0.6
dimw = w / dim 
  
fig, ax = plt.subplots() 
x = np.arange(len(data)) 
for i in range(len(data[0])):
    y = [d[i] for d in data] 
    b = ax.bar(x + i * dimw, y,  
               dimw,  
               bottom = 0.001) 
  
ax.set_xticks(x + dimw / 2) 
ax.set_xticklabels(map(str, x)) 
ax.set_yscale('log') 
  
ax.set_xlabel('x') 
ax.set_ylabel('y') 
  
ax.set_title('matplotlib.axes.Axes.bar Example') 
  
plt.show()

輸出:

範例2:

# ImpleMinetation of matplotlib function 
       
   
import numpy as np 
import matplotlib.pyplot as plt 
  
  
labels = ['Month1', 'Month2', 'Month3', 'Month4'] 
mine = [21, 52, 33, 54] 
others = [54, 23, 32, 41] 
Mine_std = [2, 3, 4, 1] 
Others_std = [3, 5, 2, 3] 
width = 0.3
  
fig, ax = plt.subplots() 
  
ax.bar(labels, mine, width,  
       yerr = Mine_std,  
       label ='Mine') 
  
ax.bar(labels, others, width,  
       yerr = Others_std, 
       bottom = mine, 
       label ='Others') 
  
ax.set_ylabel('Articles') 
ax.legend() 
  
ax.set_title('matplotlib.axes.Axes.bar Example') 
  
plt.show()

輸出:




相關用法


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