python繪制柱形圖的方法
本文實(shí)例為大家分享了python繪制柱形圖的具體代碼,供大家參考,具體內(nèi)容如下


#柱形圖
import pandas
import numpy
import matplotlib?
from matplotlib import pyplot as plt
#導(dǎo)入數(shù)據(jù)
data_columns=pandas.read_csv('D://Python projects//reference data//6.4//data.csv')
#定義中文格式
font={'family':'MicroSoft Yahei',
? ? ? 'weight':'bold',
? ? ? 'size':12}
matplotlib.rc('font',**font)
#使用手機(jī)品牌作為分組列,月消費(fèi)作為統(tǒng)計(jì)列
result_columns=data_columns.groupby(
? ? ? ? by=['手機(jī)品牌'],
? ? ? ? as_index=False)['月消費(fèi)(元)'
? ? ? ? ? ? ? ? ? ? ? ].agg({'月總消費(fèi)':numpy.sum
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? })
#生成一個(gè)間隔為1的序列
index=numpy.arange(result_columns.月總消費(fèi).size)
#繪制縱向柱形圖
plt.bar(index,result_columns['月總消費(fèi)'])
#%matplotlib qt
plt.show()
#配置顏色
maincolor=(42/256,87/256,141/256,1)
plt.bar(index,
? ? ? ? result_columns['月總消費(fèi)'])
plt.show()
#配置X軸標(biāo)簽
plt.bar(index,
? ? ? ? result_columns['月總消費(fèi)'])
plt.xticks(index,result_columns.手機(jī)品牌)
plt.show()
#對(duì)數(shù)據(jù)進(jìn)行降序排序后展示
result_asd=result_columns.sort_values(
? ? ? ? by='月總消費(fèi)',
? ? ? ? ascending=False)
plt.bar(index,
? ? ? ? result_asd.月總消費(fèi),
? ? ? ? color=maincolor)
plt.xticks(index,result_asd.手機(jī)品牌)
plt.show()結(jié)果為:

#橫向柱形圖 result_asd=result_columns.sort_values( ? ? ? ? by='月總消費(fèi)', ? ? ? ? ascending=False) plt.barh(index, ? ? ? ? result_asd.月總消費(fèi), ? ? ? ? color=maincolor) plt.yticks(index,result_asd.手機(jī)品牌) plt.show()
結(jié)果為:

#計(jì)算出交叉表的數(shù)據(jù) result=data_columns.pivot_table( ? ? ? ? values='月消費(fèi)(元)', ? ? ? ? index='手機(jī)品牌', ? ? ? ? columns='通信品牌', ? ? ? ? aggfunc=numpy.sum)
結(jié)果為:

#定義三個(gè)顏色 index=numpy.arange(len(result)) mincolor=(42/256,87/256,141/256,1/3) midcolor=(42/256,87/256,141/256,2/3) maxcolor=(42/256,87/256,141/256,1) #建立簇狀柱形圖 plt.bar( ? ? ? ? index, ? ? ? ? result['全球通'], ? ? ? ? color=mincolor, ? ? ? ? width=1/4) plt.bar( ? ? ? ? index+1/4, ? ? ? ? result['動(dòng)感地帶'], ? ? ? ? color=midcolor, ? ? ? ? width=1/4) plt.bar( ? ? ? ? index+1/2, ? ? ? ? result['神州行'], ? ? ? ? color=maxcolor, ? ? ? ? width=1/4) plt.xticks(index+1/3,result.index) #添加圖例 plt.legend(['全球通','動(dòng)感地帶','神州行']) plt.show()
結(jié)果為:

#重新排序進(jìn)行繪制 result=result.sort_values( ? ? ? ? by='神州行', ? ? ? ? ascending=False) plt.bar( ? ? ? ? index, ? ? ? ? result['全球通'], ? ? ? ? color=mincolor, ? ? ? ? width=1/4) plt.bar( ? ? ? ? index+1/4, ? ? ? ? result['動(dòng)感地帶'], ? ? ? ? color=midcolor, ? ? ? ? width=1/4) plt.bar( ? ? ? ? index+1/2, ? ? ? ? result['神州行'], ? ? ? ? color=maxcolor, ? ? ? ? width=1/4) plt.xticks(index+1/3,result.index) plt.legend(['全球通','動(dòng)感地帶','神州行']) plt.show()
結(jié)果為:

#繪制堆疊柱形圖 result=result.sort_values( ? ? ? ? by='神州行', ? ? ? ? ascending=False) plt.bar( ? ? ? ? index, ? ? ? ? result['全球通'], ? ? ? ? color=maxcolor) plt.bar( ? ? ? ? index, ? ? ? ? result['動(dòng)感地帶'], ? ? ? ? bottom=result['全球通'], ? ? ? ? color=midcolor) plt.bar( ? ? ? ? index, ? ? ? ? result['神州行'], ? ? ? ? bottom=result['全球通']+result['動(dòng)感地帶'], ? ? ? ? color=mincolor) plt.xticks(index,result.index) plt.legend(['全球通','動(dòng)感地帶','神州行']) plt.show()
結(jié)果為:

#繪制雙向柱形圖 plt.barh( ? ? ? ? index, ? ? ? ? result['神州行'], ? ? ? ? color=midcolor) plt.barh( ? ? ? ? index, ? ? ? ? -result['動(dòng)感地帶'], ? ? ? ? color=maxcolor) plt.yticks(index, ? ? ? ? ? ?result.index) plt.legend(['動(dòng)感地帶','神州行']) plt.show()
結(jié)果為:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python使用bar繪制堆積/帶誤差棒柱形圖的實(shí)現(xiàn)
- 如何利用Python連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)儲(chǔ)存
- Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄
- Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖
- 教你利用python的matplotlib(pyplot)繪制折線圖和柱狀圖
- python連接clickhouse數(shù)據(jù)庫(kù)的兩種方式小結(jié)
- python使用Matplotlib繪制多種常見(jiàn)圖形
- Python數(shù)據(jù)分析之?Matplotlib?餅圖繪制
- Python連接數(shù)據(jù)庫(kù)使用matplotlib畫柱形圖
相關(guān)文章
Python實(shí)現(xiàn)的本地文件搜索功能示例【測(cè)試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)的本地文件搜索功能,涉及Python針對(duì)文件與目錄的遍歷、判斷、編碼轉(zhuǎn)換、查找等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
python的程序分支結(jié)構(gòu)用法及說(shuō)明
這篇文章主要介紹了python的程序分支結(jié)構(gòu)用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
深入理解python對(duì)json的操作總結(jié)
Json最廣泛的應(yīng)用是作為AJAX中web服務(wù)器和客戶端的通訊的數(shù)據(jù)格式,本篇文章主要介紹了python對(duì)json的操作總結(jié),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
使用PyTorch實(shí)現(xiàn)MNIST手寫體識(shí)別代碼
今天小編就為大家分享一篇使用PyTorch實(shí)現(xiàn)MNIST手寫體識(shí)別代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python實(shí)現(xiàn)掃描日志關(guān)鍵字的示例
下面小編就為大家分享一篇python實(shí)現(xiàn)掃描日志關(guān)鍵字的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python實(shí)現(xiàn)比較兩個(gè)文件夾中代碼變化的方法
這篇文章主要介紹了Python實(shí)現(xiàn)比較兩個(gè)文件夾中代碼變化的方法,實(shí)例分析了Python讀取文件夾中文件及字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Jupyter Notebook 實(shí)現(xiàn)正常顯示中文和負(fù)號(hào)
這篇文章主要介紹了Jupyter Notebook 實(shí)現(xiàn)正常顯示中文和負(fù)號(hào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python中easy_install 和 pip 的安裝及使用
本篇文章主要介紹了Python中easy_install 和 pip 的安裝及使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

