婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

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

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

未使用numpy版本

適用于少量數據,數據結構需要手動構造。

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參數的位置
 bottom_y = [(a+b) for a, b in zip(y, bottom_y)]

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

使用numpy版本

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

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參數的位置
 bottom_y = y + bottom_y

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

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

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

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

巨人網絡通訊聲明:本文標題《matplotlib bar()實現百分比堆積柱狀圖》,本文關鍵詞  matplotlib,bar,實現,百分比,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《matplotlib bar()實現百分比堆積柱狀圖》相關的同類信息!
  • 本頁收集關于matplotlib bar()實現百分比堆積柱狀圖的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 专栏| 株洲县| 鹤峰县| 横峰县| 博客| 上栗县| 宁阳县| 贵州省| 兴宁市| 固阳县| 平阳县| 怀远县| 龙江县| 栾城县| 罗山县| 和田市| 义乌市| 泽普县| 天津市| 楚雄市| 新巴尔虎左旗| 蚌埠市| 磐安县| 大埔县| 电白县| 乐业县| 滦平县| 隆德县| 武夷山市| 西吉县| 重庆市| 罗田县| 永嘉县| 台南县| 忻州市| 崇信县| 忻州市| 丽水市| 山东省| 博白县| 休宁县|