Python+matplotlib實現(xiàn)堆疊圖的繪制
注:本文的所有數(shù)據(jù)請移步—— 參考數(shù)據(jù)
一、水平堆疊圖
堆疊圖其實就是柱狀圖的一種特殊形式
from matplotlib import pyplot as plt plt.style.use('seaborn') plt.figure(figsize=(15,9)) plt.rcParams.update({'font.family': "Microsoft YaHei"}) plt.title("中國票房2021TOP9") plt.bar(cnbodfgbsort.index,cnbodfgbsort.PERSONS) plt.bar(cnbodfgbsort.index,cnbodfgbsort.PRICE) plt.bar(cnbodfgbsort.index,cnbodfgbsort.points) plt.show()
堆疊圖效果
可以看到有部分藍色的數(shù)據(jù)被遮擋了,如果我們想全部展現(xiàn),可以:
index_x=np.arange(len(cnbodfgbsort.index)) index_x w=0.15
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(15,9)) plt.rcParams.update({'font.family': "Microsoft YaHei"}) plt.title("中國票房2021TOP9") plt.bar(index_x,cnbodfgbsort.PERSONS,width=w) plt.bar(index_x+w,cnbodfgbsort.PRICE,width=w) plt.bar(index_x+2*w,cnbodfgbsort.points,width=w) plt.show()
可以看到Excel的數(shù)據(jù)源當中BO與PRICE和PERSONS的數(shù)字相差過大,如果做堆疊圖的話,BO會將其他的都進行覆蓋,無法顯示好的效果:
因為數(shù)據(jù)相差實在太大,我們可以直接讓BO除以1000:
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(15,9)) plt.rcParams.update({'font.family': "Microsoft YaHei"}) plt.title("中國票房2021TOP9") plt.bar(cnbodfgbsort.index,cnbodfgbsort.PERSONS) plt.bar(cnbodfgbsort.index,cnbodfgbsort.PRICE) plt.bar(cnbodfgbsort.index,cnbodfgbsort.BO/1000) plt.bar(cnbodfgbsort.index,cnbodfgbsort.points) plt.show()
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(15,9)) plt.rcParams.update({'font.family': "Microsoft YaHei"}) plt.title("中國票房2021TOP9") plt.bar(index_x-w,cnbodfgbsort.BO/1000,width=w) # 直接讓BO除以1000 plt.bar(index_x,cnbodfgbsort.PERSONS,width=w) plt.bar(index_x+w,cnbodfgbsort.PRICE,width=w) plt.bar(index_x+2*w,cnbodfgbsort.points,width=w) plt.show()
二、波浪形堆疊圖
labels=['戰(zhàn)爭','愛情','動畫','動作','驚悚','劇情'] colors=['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)
labels=['戰(zhàn)爭','愛情','動畫','動作','驚悚','劇情'] colors=['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.BO/900,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)
三、加上數(shù)據(jù)標簽
plt.legend()
labels=['票房','票價','人次','評分'] colors=['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.BO/900,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors) plt.legend()
到此這篇關(guān)于Python+matplotlib實現(xiàn)堆疊圖的繪制的文章就介紹到這了,更多相關(guān)Python matplotlib堆疊圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中使用dwebsocket實現(xiàn)后端數(shù)據(jù)實時刷新
dwebsocket是Python中一款用于實現(xiàn)WebSocket協(xié)議的庫,可用于后端數(shù)據(jù)實時刷新。在Django中結(jié)合使用dwebsocket和Channels,可以實現(xiàn)前后端的實時通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實時聊天、數(shù)據(jù)監(jiān)控、在線游戲等場景2023-04-04Python 通過打碼平臺實現(xiàn)驗證碼的實現(xiàn)
這篇文章主要介紹了Python 通過打碼平臺實現(xiàn)驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05Python數(shù)據(jù)分析庫pandas基本操作方法
下面小編就為大家分享一篇Python數(shù)據(jù)分析庫pandas基本操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04