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繪制多種常見圖形
- Python數(shù)據(jù)分析之?Matplotlib?餅圖繪制
- Python連接數(shù)據(jù)庫(kù)使用matplotlib畫柱形圖
相關(guān)文章
Django數(shù)據(jù)庫(kù)連接丟失問(wèn)題的解決方法
這篇文章主要介紹了Django數(shù)據(jù)庫(kù)連接丟失問(wèn)題的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
PyQt實(shí)現(xiàn)界面翻轉(zhuǎn)切換效果
這篇文章主要為大家詳細(xì)介紹了PyQt實(shí)現(xiàn)界面翻轉(zhuǎn)切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問(wèn)題
這篇文章主要介紹了Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python實(shí)現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07

