python繪制柱形圖的方法
更新時間:2022年04月21日 12:02:04 作者:Wayne0926
這篇文章主要為大家詳細介紹了python繪制柱形圖的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python繪制柱形圖的具體代碼,供大家參考,具體內容如下


#柱形圖
import pandas
import numpy
import matplotlib?
from matplotlib import pyplot as plt
#導入數據
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()
#對數據進行降序排序后展示
result_asd=result_columns.sort_values(
? ? ? ? by='月總消費',
? ? ? ? ascending=False)
plt.bar(index,
? ? ? ? result_asd.月總消費,
? ? ? ? color=maincolor)
plt.xticks(index,result_asd.手機品牌)
plt.show()結果為:

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

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

#定義三個顏色 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()
結果為:

#重新排序進行繪制 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()
結果為:

#繪制堆疊柱形圖 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()
結果為:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

