溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在python中調用matplotlib模塊繪制柱狀圖

發(fā)布時間:2021-03-10 15:57:43 來源:億速云 閱讀:182 作者:Leah 欄目:開發(fā)技術

這期內容當中小編將會給大家?guī)碛嘘P怎么在python中調用matplotlib模塊繪制柱狀圖,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

# bar(left, height, width=0.8, bottom=None, hold=None, **kwargs) 
# 繪制柱形圖 
# left:柱形圖的x坐標 
# height柱形圖的高度,以0.0為基準 
# width:柱形圖的寬度,默認0.8 
# facecolor:顏色 
# edgecolor:邊框顏色n 
# bottom:表示底部從y軸的哪個刻度開始畫 
# yerr:應該是對應的數據的誤差范圍,加上這個參數,柱狀圖頭部會有一個藍色的范圍標識,標出允許的誤差范圍,在水平柱狀圖中這個參數為xerr

在這里我一般特別喜歡將柱狀圖的邊緣顏色設置為白色,因為這樣畫出來比較好看

eg.

plt.bar(x,+y1,width=0.8,facecolor="#9999ff",edgecolor="white",yerr=error)

下面來說一下畫bar chart 的步驟

首先我們需要引入兩個模塊:

import numpy as np 
import matplotlib.pyplot as plt 

import numpy as np 
import matplotlib.pyplot as plt 
n = 12 
# 生成一個1-12的列表,不包括12,[ 0 1 2 3 4 5 6 7 8 9 10 11] 
x = np.arange(n) 
# np.random.uniform(0.5,1.0,n),生成n個0.5-1.0之間的隨機數 
y1 = 3 * np.random.uniform(0.5,1.0,n) 
y2 = 3 * np.random.uniform(0.5,1.0,n) 
# 在這里我們是使用一個隨機生成函數生成了兩組y的值,生成的這個隨機數是服從均勻分布的
# 如果我們的數值比較少我們可以直接給y賦值
# y = [5,7,3]

# 生成一個包含有n個值,均為0.2的list,表示允許的誤差范圍[-0.2,0.2] 
error = [0.2,] * n 

# bar(left, height, width=0.8, bottom=None, hold=None, **kwargs) 
# 繪制柱形圖 
# left:柱形圖的x坐標 
# height柱形圖的高度,以0.0為基準 
# width:柱形圖的寬度,默認0.8 
# facecolor:顏色 
# edgecolor:邊框顏色n 
# bottom:表示底部從y軸的哪個刻度開始畫 
# yerr:應該是對應的數據的誤差范圍,加上這個參數,柱狀圖頭部會有一個藍色的范圍標識,標出允許的誤差范圍,在水平柱狀圖中這個參數為xerr 
plt.bar(x,+y1,width=0.8,facecolor="#9999ff",edgecolor="white",yerr=error) 
plt.bar(x,-y2,facecolor="#ff9999",edgecolor="white") 
# 繪制文字,顯示柱狀圖形的值 
for x,y1,y2 in zip(x,y1,y2): 
 plt.text(x+0.4,y1+0.05,'%.2f' % y1,ha='center',va='bottom') 
 plt.text(x+0.4,-(y2+0.05),'%.2f' % y2,ha='center',va='top') 

plt.ylim(-3.5,3.5) 
plt.show() 

如果我們需要的是給我們柱狀圖繪制一些標記,比如橫坐標和縱坐標的值,這個時候我們可以像下面這樣做。這個例子我用的是官網上的代碼。

# Credit: Josh Hemann

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from collections import namedtuple


n_groups = 5

means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)

means_women = (25, 32, 34, 20, 25)
std_women = (3, 5, 2, 3, 3)

fig, ax = plt.subplots()

index = np.arange(n_groups)
bar_width = 0.35

opacity = 0.4
error_config = {'ecolor': '0.3'}

rects1 = ax.bar(index, means_men, bar_width,
    alpha=opacity, color='b',
    yerr=std_men, error_kw=error_config,
    label='Men')

rects2 = ax.bar(index + bar_width, means_women, bar_width,
    alpha=opacity, color='r',
    yerr=std_women, error_kw=error_config,
    label='Women')

ax.set_xlabel('Group')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
ax.legend()

fig.tight_layout()
plt.show()

在這里我們設置的X的坐標以及上邊的標簽,我們主要的代碼是:

ax.bar(index, means_men, bar_width,
    alpha=opacity, color='b',
    yerr=std_men, error_kw=error_config,
    label='Men')

ax.set_xticks(index + bar_width / 2) # 設置坐標的其實坐標
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))

上述就是小編為大家分享的怎么在python中調用matplotlib模塊繪制柱狀圖了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI