这里放上我比较喜欢的一种条形图设置,使用的是之前爬取的重庆地区链家二手房数据,数据如下:

链接:https://pan.baidu.com/s/17CMwUAdseO8tJWHEQiA8_A

提取码:dl2g

importpandas as pdimportmatplotlib.pyplot as pltimportseaborn as sns

df= pd.read_csv('lianjia_utf.csv')

df.head()

1600350-20190701213930386-269951648.png

sns.set(style='white', font_scale=1.2)#保证可以显示中文字体

plt.rcParams['font.sans-serif']='simhei'

#设置字体大小

font1 = {'family' : 'simhei','weight' : 'normal','size' : 18,}#使用数据透视表

region_pivot = pd.pivot_table(df, values='Price', index='Region', aggfunc='count').reset_index().sort_values(ascending=True,by='Price')

f, ax= plt.subplots(figsize=(8,6))#画条形图

barh = plt.barh(region_pivot['Region'].values,region_pivot['Price'].values, color='dodgerblue')

barh[-1].set_color('green')#给条形图添加数据标注

for y, x in enumerate(region_pivot['Price'].values):

plt.text(x+500, y-0.2, "%s" %x)#删除所有边框

ax.spines['right'].set_visible(False)

ax.spines['top'].set_visible(False)

ax.spines['bottom'].set_visible(False)

ax.spines['left'].set_visible(False)#ax.set(title='重庆各区域二手房总价', xlabel='地区', ylabel='总价')

plt.tick_params(labelsize=14)

plt.xlabel('地区', font1)

plt.ylabel('总价', font1)

plt.title('重庆各区域二手房总价', font1)

f.savefig('1.png', bbox_inches='tight')

运行结果如下:

1600350-20190701214140215-1045786300.png

如果把上图改成柱形图,可以这样做:

sns.set(style='white', font_scale=1.2)#保证可以显示中文字体

plt.rcParams['font.sans-serif']='simhei'

#设置字体大小

font1 = {'family' : 'simhei','weight' : 'normal','size' : 18,}#使用数据透视表

region_pivot = pd.pivot_table(df, values='Price', index='Region', aggfunc='count').reset_index().sort_values(ascending=False,by='Price')

f, ax= plt.subplots(figsize=(12,6))#画柱形图

bar = plt.bar(region_pivot['Region'].values,region_pivot['Price'].values, color='dodgerblue')

bar[0].set_color('green')#给条形图添加数据标注

for x, y in enumerate(region_pivot['Price'].values):

plt.text(x-0.4, y+500, "%s" %y)#删除所有边框

ax.spines['right'].set_visible(False)

ax.spines['top'].set_visible(False)

ax.spines['bottom'].set_visible(False)

ax.spines['left'].set_visible(False)#ax.set(title='重庆各区域二手房总价', xlabel='地区', ylabel='总价')

plt.tick_params(labelsize=14)

plt.xlabel('地区', font1)

plt.ylabel('总价', font1)

plt.title('重庆各区域二手房总价', font1)

f.savefig('1_1.png', bbox_inches='tight')

运行结果如下:

1600350-20190701215846064-987617972.png

关于配色,matplotlib中的配色,可参考下图:

1600350-20190701214412069-166384857.png

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐