Python+matplotlib實(shí)現(xiàn)堆疊圖的繪制
注:本文的所有數(shù)據(jù)請(qǐng)移步—— 參考數(shù)據(jù)
一、水平堆疊圖
堆疊圖其實(shí)就是柱狀圖的一種特殊形式
from matplotlib import pyplot as plt plt.style.use('seaborn') plt.figure(figsize=(15,9)) plt.rcParams.update({'font.family': "Microsoft YaHei"}) plt.title("中國(guó)票房2021TOP9") plt.bar(cnbodfgbsort.index,cnbodfgbsort.PERSONS) plt.bar(cnbodfgbsort.index,cnbodfgbsort.PRICE) plt.bar(cnbodfgbsort.index,cnbodfgbsort.points) plt.show()
堆疊圖效果
可以看到有部分藍(lán)色的數(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("中國(guó)票房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ù)源當(dāng)中BO與PRICE和PERSONS的數(shù)字相差過(guò)大,如果做堆疊圖的話,BO會(huì)將其他的都進(jìn)行覆蓋,無(wú)法顯示好的效果:
因?yàn)閿?shù)據(jù)相差實(shí)在太大,我們可以直接讓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("中國(guó)票房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("中國(guó)票房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)爭(zhēng)','愛情','動(dòng)畫','動(dòng)作','驚悚','劇情'] colors=['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)
labels=['戰(zhàn)爭(zhēng)','愛情','動(dòng)畫','動(dòng)作','驚悚','劇情'] 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ù)標(biāo)簽
plt.legend()
labels=['票房','票價(jià)','人次','評(píng)分'] 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實(shí)現(xiàn)堆疊圖的繪制的文章就介紹到這了,更多相關(guān)Python matplotlib堆疊圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python的scikit-image模塊實(shí)例講解
在本篇文章里小編給大家整理了一篇關(guān)于Python的scikit-image模塊實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-12-12Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新
dwebsocket是Python中一款用于實(shí)現(xiàn)WebSocket協(xié)議的庫(kù),可用于后端數(shù)據(jù)實(shí)時(shí)刷新。在Django中結(jié)合使用dwebsocket和Channels,可以實(shí)現(xiàn)前后端的實(shí)時(shí)通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實(shí)時(shí)聊天、數(shù)據(jù)監(jiān)控、在線游戲等場(chǎng)景2023-04-04Python輸入輸出從鍵盤到文件實(shí)戰(zhàn)全面指南
這篇文章主要為大家介紹了Python輸入輸出從鍵盤到文件實(shí)戰(zhàn)全面指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Django 導(dǎo)出 Excel 代碼的實(shí)例詳解
本篇文章主要介紹了Django 導(dǎo)出 Excel 代碼的實(shí)例詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Python 通過(guò)打碼平臺(tái)實(shí)現(xiàn)驗(yàn)證碼的實(shí)現(xiàn)
這篇文章主要介紹了Python 通過(guò)打碼平臺(tái)實(shí)現(xiàn)驗(yàn)證碼的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Python數(shù)據(jù)分析庫(kù)pandas基本操作方法
下面小編就為大家分享一篇Python數(shù)據(jù)分析庫(kù)pandas基本操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04