matplotlib-实战01

# 1、可视化实战
df_mean = df.drop(['city_property','city_name','house_title','house_age','within_area'], axis=1).groupby('region_1_name').mean().sort_values('deal_price',ascending=False)
df_mean
# drop(默认axis=0)删掉行,axis=1删掉列
# groupby汇总

# 2、
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

# 3、用于正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 解决中文字体下坐标轴负数的负号显示问题
plt.rcParams['axes.unicode_minus'] = False

# 4、设置画图的背景格式(画布风格ggplot)
mpl.style.use('ggplot')
# 设定一张名为fig的画布,大画布分为ax1和ax2两个小画布,figsize设定画布fig大小12*4(point)
fig,(ax1,ax2) = plt.subplots(1,2,figsize=(16,6))
# 在数据集后用.plot()方法就可以画图了,kind="barh"表画一张条形图,
# ax=ax1表示条形图画在ax1这张画布上,可以用set_xlabel设定X轴标签。
df_mean['deal_price'].plot(kind='barh',ax=ax1)
ax1.set_xlabel('郑州市各地区房源成交价格平均值')
df_mean['list_Price'].plot(kind='barh',ax=ax2)
ax2.set_xlabel('郑州市各地区房源挂牌价格平均值')

# 设定好图表元素后,使图表自动调整格式
fig.tight_layout()


# 5、指定一个2*2布局
fig,axes = plt.subplots(2,2,figsize=(10,10))

s = df_mean['deal_price']
# kind='line'折线图,kind='bar'柱状图,kind='box'箱形图,kind='pie'饼图
s.plot(ax=axes[0][0],kind='line',title='line')
s.plot(ax=axes[0][1],kind='bar',title='bar')
s.plot(ax=axes[1][0],kind='box',title='box')
s.plot(ax=axes[1][1],kind='pie',title='pie')

fig.tight_layout()



# 6、画出成交价格与浏览次数的散点图
fig,ax = plt.subplots(1,1,figsize=(12,4))
# 画散点图时,将图表的类型作为一种方法,参数是要交叉的两个数据集(度量)。
# 在画散点图时,在画布ax上用scatter()方法。
ax.scatter(df['deal_price'],df['through_nums'])
# 
ax.set_xlabel("deal_price")
ax.set_ylabel("through_nums")
原文地址:https://www.cnblogs.com/mrfanqie/p/matplotlib_202102191451.html