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軸上的坐標與柱體對其的位置 color = ‘orangered', alpha = 0.6, #柱體透明度 label = ‘收入(剔除自己轉(zhuǎn)入)') plt.bar(x,y3,width = bar_with, bottom = y1, #柱體基線的y軸坐標 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軸標題 plt.xticks(x + bar_with/2, tick_label, rotation = 70) #設(shè)置x軸坐標 plt.xlabel(‘時間',fontsize = 8, verticalalignment = ‘top', horizontalalignment=‘right',rotation=‘horizontal') plt.xlabel(‘時間',fontsize = 8, verticalalignment = ‘bottom', horizontalalignment=‘center') #圖例設(shè)在圖形外面,控制坐標參數(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繪圖也沒有哪么辣眼睛了。。。
到此這篇關(guān)于python繪制堆疊條形圖介紹的文章就介紹到這了,更多相關(guān)python堆疊條形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 中的判斷語句,循環(huán)語句,函數(shù)
這篇文章主要介紹了Python 中的判斷語句,循環(huán)語句,函數(shù),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08Python Pandas常用函數(shù)方法總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Pandas常用函數(shù)方法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python中強大的函數(shù)map?filter?reduce使用詳解
Python是一門功能豐富的編程語言,提供了許多內(nèi)置函數(shù),以簡化各種編程任務(wù),在Python中,map(),filter()和reduce()是一組非常有用的函數(shù),它們允許對可迭代對象進行操作,從而實現(xiàn)數(shù)據(jù)轉(zhuǎn)換、篩選和累積等操作,本文將詳細介紹這三個函數(shù),包括它們的基本用法和示例代碼2023-11-11Python爬蟲學(xué)習(xí)之獲取指定網(wǎng)頁源碼
這篇文章主要為大家詳細介紹了Python爬蟲學(xué)習(xí)之獲取指定網(wǎng)頁源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別
這篇文章主要為大家詳細介紹了基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05