濮阳杆衣贸易有限公司

主頁 > 知識庫 > matplotlib bar()實現(xiàn)百分比堆積柱狀圖

matplotlib bar()實現(xiàn)百分比堆積柱狀圖

熱門標簽:柳州正規(guī)電銷機器人收費 騰訊地圖標注有什么版本 千呼ai電話機器人免費 深圳網(wǎng)絡外呼系統(tǒng)代理商 高德地圖標注字母 外呼系統(tǒng)前面有錄音播放嗎 400電話辦理費用收費 申請辦個400電話號碼 鎮(zhèn)江人工外呼系統(tǒng)供應商

使用matplotlib創(chuàng)建百分比堆積柱狀圖的思路與堆積柱狀圖類似,只不過bottom參數(shù)累計的不是數(shù)值而是百分比,因此,需要事先計算每組柱子的數(shù)值總和,進而求百分比。

未使用numpy版本

適用于少量數(shù)據(jù),數(shù)據(jù)結構需要手動構造。

import matplotlib.pyplot as plt

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
first = [20, 34, 30, 35, 27]
second = [25, 32, 34, 20, 25]
third = [21, 31, 37, 21, 28]
fourth = [26, 31, 35, 27, 21]
data = [first, second, third, fourth]

x = range(len(labels))
width = 0.35

# 將bottom_y元素都初始化為0
bottom_y = [0] * len(labels)
# 計算每組柱子的總和,為計算百分比做準備
sums = [sum(i) for i in zip(first, second, third, fourth)]
for i in data:
 # 計算每個柱子的高度,即百分比
 y = [a/b for a, b in zip(i, sums)]
 plt.bar(x, y, width, bottom=bottom_y)
 # 計算bottom參數(shù)的位置
 bottom_y = [(a+b) for a, b in zip(y, bottom_y)]

plt.xticks(x, labels)
plt.title('Percent stacked bar ')
plt.show()

使用numpy版本

第一個版本的缺陷在于數(shù)據(jù)需要手動構造,而且計算稍微繁瑣一些。
使用numpy便于處理規(guī)模比較大且已存儲在文件中數(shù)據(jù)的數(shù)據(jù),計算更簡便。

import numpy as np
import matplotlib.pyplot as plt

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
first = [20, 34, 30, 35, 27]
second = [25, 32, 34, 20, 25]
third = [21, 31, 37, 21, 28]
fourth = [26, 31, 35, 27, 21]
data = [first, second, third, fourth]

x = range(len(labels))
width = 0.35

# 將bottom_y元素都初始化為0
bottom_y = np.zeros(len(labels))
data = np.array(data)
# 按列計算計算每組柱子的總和,為計算百分比做準備
sums = np.sum(data, axis=0)
for i in data:
 # 計算每個柱子的高度,即百分比
 y = i / sums
 plt.bar(x, y, width, bottom=bottom_y)
 # 計算bottom參數(shù)的位置
 bottom_y = y + bottom_y

plt.xticks(x, labels)
plt.title('Percent stacked bar ')
plt.show()

到此這篇關于matplotlib bar()實現(xiàn)百分比堆積柱狀圖 的文章就介紹到這了,更多相關matplotlib 百分比堆積柱狀圖 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • matplotlib bar()實現(xiàn)多組數(shù)據(jù)并列柱狀圖通用簡便創(chuàng)建方法
  • Python matplotlib模塊及柱狀圖用法解析
  • python調(diào)用matplotlib模塊繪制柱狀圖
  • python使用matplotlib畫柱狀圖、散點圖
  • python+matplotlib實現(xiàn)禮盒柱狀圖實例代碼
  • python使用matplotlib繪制柱狀圖教程

標簽:海南 平頂山 郴州 合肥 哈爾濱 烏蘭察布 大慶 烏蘭察布

巨人網(wǎng)絡通訊聲明:本文標題《matplotlib bar()實現(xiàn)百分比堆積柱狀圖》,本文關鍵詞  matplotlib,bar,實現(xiàn),百分比,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《matplotlib bar()實現(xiàn)百分比堆積柱狀圖》相關的同類信息!
  • 本頁收集關于matplotlib bar()實現(xiàn)百分比堆積柱狀圖的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    河源市| 长泰县| 西峡县| 吴堡县| 曲松县| 马山县| 德化县| 铜陵市| 宝鸡市| 利川市| 乌拉特后旗| 奉化市| 西乌| 洞口县| 荆门市| 临湘市| 广元市| 台湾省| 北票市| 长岛县| 怀化市| 和林格尔县| 汉寿县| 柞水县| 临高县| 安多县| 康定县| 无为县| 聊城市| 锦州市| 广河县| 西藏| 班玛县| 鄄城县| 巴林左旗| 托克逊县| 武鸣县| 阿合奇县| 海阳市| 资阳市| 榆中县|