python繪制堆疊條形圖介紹
目前在網(wǎng)絡(luò)上多是單個條形圖堆疊,沒看到一組的條形圖堆疊。
代碼如下:
import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker
導(dǎo)入一組自己造的數(shù)據(jù)
data = pd.read_excel('data.xlsx')
In [4]: data
Out[4]:

多使用幾個plt.bar()函數(shù),就可以畫出來啦。。。
tick_label = list(data.columns) tick_label.remove(‘類別') x = np.arange(len(tick_label)) y1 = data.iloc[2,1:].values.tolist() #收入(剔除自己轉(zhuǎn)入) y2 = data.iloc[3,1:].values.tolist() #支出(剔除自己轉(zhuǎn)入) y3 = data.iloc[4,1:].values.tolist() #收入(自己轉(zhuǎn)入) y4 = data.iloc[5,1:].values.tolist() #支出(自己轉(zhuǎn)入) bar_with = 0.25 #柱體寬度plt.figure(figsize = (12,6)) #畫布大小 plt.bar(x, y1, width = bar_with, #柱體寬度 align = ‘center', #x軸上的坐標(biāo)與柱體對其的位置 color = ‘orangered', alpha = 0.6, #柱體透明度 label = ‘收入(剔除自己轉(zhuǎn)入)') plt.bar(x,y3,width = bar_with, bottom = y1, #柱體基線的y軸坐標(biāo) align = ‘center', color = ‘lightsalmon', alpha = 0.6, label = ‘收入(自己轉(zhuǎn)入)') plt.bar(x + bar_with, y2, width = bar_with, align = ‘center', color = ‘deepskyblue', alpha = 0.6, label = ‘支出(剔除自己轉(zhuǎn)入)') plt.bar(x + bar_with, y4, width = bar_with, bottom = y2, align = ‘center', color = ‘lightskyblue', alpha = 0.6, label = ‘支出(自己轉(zhuǎn)入)') plt.title(‘月度收支表', fontsize = 10) #設(shè)置x軸標(biāo)題 plt.xticks(x + bar_with/2, tick_label, rotation = 70) #設(shè)置x軸坐標(biāo) plt.xlabel(‘時間',fontsize = 8, verticalalignment = ‘top', horizontalalignment=‘right',rotation=‘horizontal') plt.xlabel(‘時間',fontsize = 8, verticalalignment = ‘bottom', horizontalalignment=‘center') #圖例設(shè)在圖形外面,控制坐標(biāo)參數(shù) plt.legend(loc = ‘center', bbox_to_anchor = (0.77, 1.1), ncol=2) plt.savefig(‘draw_bar.png', dpi=200, bbox_inches = ‘tight') plt.close()
繪制如圖:

是不是其實(shí)plt繪圖也沒有哪么辣眼睛了。。。
到此這篇關(guān)于python繪制堆疊條形圖介紹的文章就介紹到這了,更多相關(guān)python堆疊條形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python繪制百分比堆疊柱狀圖并填充圖案
- Python?OpenCV超詳細(xì)講解圖像堆疊的實(shí)現(xiàn)
- Python+matplotlib實(shí)現(xiàn)堆疊圖的繪制
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
- Python圖像處理之圖片拼接和堆疊案例教程
- python numpy 矩陣堆疊實(shí)例
- Python 堆疊柱狀圖繪制方法
- Python繪制堆疊柱狀圖的實(shí)例
- python3+PyQt5+Qt Designer實(shí)現(xiàn)堆疊窗口部件
- python+opencv實(shí)現(xiàn)堆疊圖片
相關(guān)文章
Django Admin后臺添加數(shù)據(jù)庫視圖過程解析
這篇文章主要介紹了Django Admin后臺添加數(shù)據(jù)庫視圖過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
pycharm社區(qū)版安裝node.js插件運(yùn)行js代碼方法
PyCharm可以說是當(dāng)今最流行的一款Python IDE了,下面這篇文章主要給大家介紹了關(guān)于pycharm社區(qū)版安裝node.js插件運(yùn)行js代碼的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例
這篇文章主要介紹了Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息,涉及Python基于Selenium模塊的模擬瀏覽器登陸、解析、抓取信息,以及MongoDB數(shù)據(jù)庫的連接、寫入等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
使用Python編程分析火爆全網(wǎng)的魷魚游戲豆瓣影評
本文來為大家介紹如何使用Python爬取影評的操作,主要是爬取《魷魚游戲》在豆瓣上的一些影評,對數(shù)據(jù)做一些簡單的分析,用數(shù)據(jù)的角度重新審視下這部劇,有需要的朋友可以借鑒參考下2021-10-10

