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)文章
Python 中的判斷語句,循環(huán)語句,函數(shù)
這篇文章主要介紹了Python 中的判斷語句,循環(huán)語句,函數(shù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08Python Pandas常用函數(shù)方法總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Pandas常用函數(shù)方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python try except異常捕獲機(jī)制原理解析
這篇文章主要介紹了Python try except異常捕獲機(jī)制原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Python中強(qiáng)大的函數(shù)map?filter?reduce使用詳解
Python是一門功能豐富的編程語言,提供了許多內(nèi)置函數(shù),以簡化各種編程任務(wù),在Python中,map(),filter()和reduce()是一組非常有用的函數(shù),它們允許對可迭代對象進(jìn)行操作,從而實(shí)現(xiàn)數(shù)據(jù)轉(zhuǎn)換、篩選和累積等操作,本文將詳細(xì)介紹這三個函數(shù),包括它們的基本用法和示例代碼2023-11-11Python爬蟲學(xué)習(xí)之獲取指定網(wǎng)頁源碼
這篇文章主要為大家詳細(xì)介紹了Python爬蟲學(xué)習(xí)之獲取指定網(wǎng)頁源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別
這篇文章主要為大家詳細(xì)介紹了基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05