python繪制堆疊條形圖介紹
更新時間:2021年12月19日 15:34:06 作者:lingan_Hong
大家好,本篇文章主要講的是python繪制堆疊條形圖介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
目前在網絡上多是單個條形圖堆疊,沒看到一組的條形圖堆疊。
代碼如下:
import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker
導入一組自己造的數(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() #收入(剔除自己轉入) y2 = data.iloc[3,1:].values.tolist() #支出(剔除自己轉入) y3 = data.iloc[4,1:].values.tolist() #收入(自己轉入) y4 = data.iloc[5,1:].values.tolist() #支出(自己轉入) bar_with = 0.25 #柱體寬度plt.figure(figsize = (12,6)) #畫布大小 plt.bar(x, y1, width = bar_with, #柱體寬度 align = ‘center', #x軸上的坐標與柱體對其的位置 color = ‘orangered', alpha = 0.6, #柱體透明度 label = ‘收入(剔除自己轉入)') plt.bar(x,y3,width = bar_with, bottom = y1, #柱體基線的y軸坐標 align = ‘center', color = ‘lightsalmon', alpha = 0.6, label = ‘收入(自己轉入)') plt.bar(x + bar_with, y2, width = bar_with, align = ‘center', color = ‘deepskyblue', alpha = 0.6, label = ‘支出(剔除自己轉入)') plt.bar(x + bar_with, y4, width = bar_with, bottom = y2, align = ‘center', color = ‘lightskyblue', alpha = 0.6, label = ‘支出(自己轉入)') plt.title(‘月度收支表', fontsize = 10) #設置x軸標題 plt.xticks(x + bar_with/2, tick_label, rotation = 70) #設置x軸坐標 plt.xlabel(‘時間',fontsize = 8, verticalalignment = ‘top', horizontalalignment=‘right',rotation=‘horizontal') plt.xlabel(‘時間',fontsize = 8, verticalalignment = ‘bottom', horizontalalignment=‘center') #圖例設在圖形外面,控制坐標參數(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()
繪制如圖:

是不是其實plt繪圖也沒有哪么辣眼睛了。。。
到此這篇關于python繪制堆疊條形圖介紹的文章就介紹到這了,更多相關python堆疊條形圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python 中的判斷語句,循環(huán)語句,函數(shù)
這篇文章主要介紹了Python 中的判斷語句,循環(huán)語句,函數(shù),文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
Python中強大的函數(shù)map?filter?reduce使用詳解
Python是一門功能豐富的編程語言,提供了許多內置函數(shù),以簡化各種編程任務,在Python中,map(),filter()和reduce()是一組非常有用的函數(shù),它們允許對可迭代對象進行操作,從而實現(xiàn)數(shù)據(jù)轉換、篩選和累積等操作,本文將詳細介紹這三個函數(shù),包括它們的基本用法和示例代碼2023-11-11

