这篇文章给大家分享的是有关Python数据可视化案例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
前言
三个步骤:
确定问题,选择图形
转换数据,应用函数
参数设置,一目了然

首先对时段进行分析
提出问题:租赁总量对应湿度的变化趋势
适合图形:因为湿度属于连续性数值变量,我们可以选择折线图反应变化趋势
转换数据:我们需要一个二维数据框,按照温度变化排序,取对应的三个租赁数的平均值
应用函数:直接应用plt的plot函数即可完成折线图
workingday_df = Bikedata[Bikedata['workingday']==1]#t
workingday_df = workingday_df.groupby(['hour'],as_index=True).agg({'count':'mean','registered':'mean','casual':'mean'})
nworkingday_df = Bikedata[Bikedata['workingday']==0]
nworkingday_df = nworkingday_df.groupby(['hour'],as_index=True).agg({'count':'mean','registered':'mean','casual':'mean'})
nworkingday_df.head()
![]()
figure,axes = plt.subplots(1,2,sharey=True)#设置一个1*2的画布,且共享y轴
workingday_df.plot(figsize=(15,5),title='The average number of rentals initiated per hour in the working day',ax=axes[0])
nworkingday_df.plot(figsize=(15,5),title='The average number of rentals initiated per hour in the nworking day',ax=axes[1])
<matplotlib.axes._subplots.AxesSubplot at 0xe452940>
可以看出:
对温度进行分析
提出问题:租赁总量对应湿度的变化趋势
适合图形:因为湿度属于连续性数值变量,我们可以选择折线图反应变化趋势
转换数据:我们需要一个二维数据框,按照温度变化排序,取对应的三个租赁数的平均值
应用函数:直接应用plt的plot函数即可完成折线图
参数设置:只需要设置折线图的标题,其他参数默认
temp_df = Bikedata.groupby(['temp'],as_index='True').agg({'count':'mean','registered':'mean','casual':'mean'})
temp_df.plot(title = 'The average number of rentals initiated per hour changes with the temperature')
<matplotlib.axes._subplots.AxesSubplot at 0xe57d7f0>

湿度对租赁数量的影响
提出问题:租赁总量对应湿度的变化趋势
适合图形:因为湿度属于连续性数值变量,我们可以选择折线图反应变化趋势
转换数据:我们需要一个二维数据框,按照温度变化排序,取对应的三个租赁数的平均值
应用函数:直接应用plt的plot函数即可完成折线图
参数设置:只需要设置折线图的标题,其他参数默认
humidity_df = Bikedata.groupby(['humidity'],as_index=True).agg({'count':'mean','registered':'mean','casual':'mean'})
humidity_df.plot(title='Average number of rentals initiated per hour in different humidity')
<matplotlib.axes._subplots.AxesSubplot at 0xe582400>
![]()
可以观察到在湿度20左右租赁数量迅速达到高峰值,此后缓慢递减。
年份,月份和季节作图方法类似,都采用折线图绘制,这里省略。
查看不同天气对出行情况的影响
提出问题:租赁总量对应湿度的变化趋势
适合图形:因为天气情况属于数值型分类变量,我们可以选择柱形图观察数量分布
转换数据:我们需要一个二维数据框,按照天气情况对租赁数量取平均值
应用函数:应用plt的plot.bar函数绘制组合柱形图
参数设置:只需要设置折线图的标题,其他参数默认
weather_df = Bikedata.groupby(['weather'],as_index=True).agg({'registered':'mean','casual':'mean'})
weather_df.plot.bar(stacked=True,title='Average number of rentals initiated per hour in different weather')
<matplotlib.axes._subplots.AxesSubplot at 0xe7e0a90>

观察到天气等级为4时,平均出行人数比天气等级为2是还要高,这不符合常理
我们查看一下天气等级为4的详细情况
count_weather = Bikedata.groupby('weather')
count_weather[['casual','registered','count']].count()

时间为工作日的下午六点钟,属于晚高峰异常数据,不具有代表性。
会员用户和临时用户在整体用户中占比
提出问题:查看会员用户和临时用户在整体用户中的比例
适合图形:查看占比,适合用饼图pie
转换数据:需要一个二维数据框,按天数取两种用户的平均值
应用函数:应用plt的plot.pie函数绘制饼图
参数设置:这是数据标签和类别标签
#考虑到相同日期是否工作日,星期几,以及所属年份等信息是一样的,把租赁数据按天求和,其它日期类数据取平均值
day_df = Bikedata.groupby(['date'], as_index=False).agg({'casual':'sum','registered':'sum','count':'sum', 'workingday':'mean','weekday':'mean','holiday':'mean','year':'mean'})
day_df.head()
![]()
#按天取两种类型用户平均值
number_pei=day_df[['casual','registered']].mean()
number_pei
casual 517.411765
registered 2171.067031
dtype: float64
#绘制饼图
plt.axes(aspect='equal')
plt.pie(number_pei, labels=['casual','registered'], autopct='%1.1f%%', pctdistance=0.6 , labeldistance=1.05 , radius=1 )
plt.title('Casual or registered in the total lease')
Text(0.5,1,'Casual or registered in the total lease')

感谢各位的阅读!关于“Python数据可视化案例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!