python繪制柱形圖的方法
更新時間:2022年04月21日 12:02:04 作者:Wayne0926
這篇文章主要為大家詳細介紹了python繪制柱形圖的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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) #使用手機品牌作為分組列,月消費作為統(tǒng)計列 result_columns=data_columns.groupby( ? ? ? ? by=['手機品牌'], ? ? ? ? as_index=False)['月消費(元)' ? ? ? ? ? ? ? ? ? ? ? ].agg({'月總消費':numpy.sum ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }) #生成一個間隔為1的序列 index=numpy.arange(result_columns.月總消費.size) #繪制縱向柱形圖 plt.bar(index,result_columns['月總消費']) #%matplotlib qt plt.show() #配置顏色 maincolor=(42/256,87/256,141/256,1) plt.bar(index, ? ? ? ? result_columns['月總消費']) plt.show() #配置X軸標簽 plt.bar(index, ? ? ? ? result_columns['月總消費']) plt.xticks(index,result_columns.手機品牌) plt.show() #對數(shù)據(jù)進行降序排序后展示 result_asd=result_columns.sort_values( ? ? ? ? by='月總消費', ? ? ? ? ascending=False) plt.bar(index, ? ? ? ? result_asd.月總消費, ? ? ? ? color=maincolor) plt.xticks(index,result_asd.手機品牌) plt.show()
結(jié)果為:
#橫向柱形圖 result_asd=result_columns.sort_values( ? ? ? ? by='月總消費', ? ? ? ? ascending=False) plt.barh(index, ? ? ? ? result_asd.月總消費, ? ? ? ? color=maincolor) plt.yticks(index,result_asd.手機品牌) plt.show()
結(jié)果為:
#計算出交叉表的數(shù)據(jù) result=data_columns.pivot_table( ? ? ? ? values='月消費(元)', ? ? ? ? index='手機品牌', ? ? ? ? columns='通信品牌', ? ? ? ? aggfunc=numpy.sum)
結(jié)果為:
#定義三個顏色 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['動感地帶'], ? ? ? ? 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(['全球通','動感地帶','神州行']) plt.show()
結(jié)果為:
#重新排序進行繪制 result=result.sort_values( ? ? ? ? by='神州行', ? ? ? ? ascending=False) plt.bar( ? ? ? ? index, ? ? ? ? result['全球通'], ? ? ? ? color=mincolor, ? ? ? ? width=1/4) plt.bar( ? ? ? ? index+1/4, ? ? ? ? result['動感地帶'], ? ? ? ? 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(['全球通','動感地帶','神州行']) plt.show()
結(jié)果為:
#繪制堆疊柱形圖 result=result.sort_values( ? ? ? ? by='神州行', ? ? ? ? ascending=False) plt.bar( ? ? ? ? index, ? ? ? ? result['全球通'], ? ? ? ? color=maxcolor) plt.bar( ? ? ? ? index, ? ? ? ? result['動感地帶'], ? ? ? ? bottom=result['全球通'], ? ? ? ? color=midcolor) plt.bar( ? ? ? ? index, ? ? ? ? result['神州行'], ? ? ? ? bottom=result['全球通']+result['動感地帶'], ? ? ? ? color=mincolor) plt.xticks(index,result.index) plt.legend(['全球通','動感地帶','神州行']) plt.show()
結(jié)果為:
#繪制雙向柱形圖 plt.barh( ? ? ? ? index, ? ? ? ? result['神州行'], ? ? ? ? color=midcolor) plt.barh( ? ? ? ? index, ? ? ? ? -result['動感地帶'], ? ? ? ? color=maxcolor) plt.yticks(index, ? ? ? ? ? ?result.index) plt.legend(['動感地帶','神州行']) plt.show()
結(jié)果為:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python使用bar繪制堆積/帶誤差棒柱形圖的實現(xiàn)
- 如何利用Python連接MySQL數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)儲存
- Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫簡單實現(xiàn)注冊登錄
- Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖
- 教你利用python的matplotlib(pyplot)繪制折線圖和柱狀圖
- python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié)
- python使用Matplotlib繪制多種常見圖形
- Python數(shù)據(jù)分析之?Matplotlib?餅圖繪制
- Python連接數(shù)據(jù)庫使用matplotlib畫柱形圖
相關(guān)文章
python實現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張
這篇文章主要為大家詳細介紹了python實現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07