美文网首页工具癖数据分析工具pandas快速入门教程
数据分析工具pandas快速入门教程3绘图2matplotlib

数据分析工具pandas快速入门教程3绘图2matplotlib

作者: python测试开发 | 来源:发表于2018-08-23 12:39 被阅读7次

matplotlib统计图

seaborn的tips数据集包含消费账单的大小,人数,星期几,时间等。

  • 加载tips数据集
tips = sns.load_dataset("tips")

print(tips.head())
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4
  • 直方图(单变量)
fig = plt.figure()
axes1 = fig.add_subplot(1, 1, 1)
axes1.hist(tips['total_bill'], bins=10)
axes1.set_title('Histogram of Total Bill')
axes1.set_xlabel('Frequency' )
axes1.set_ylabel('Total Bill')
fig.show ()
图片.png
  • 散点图(双变量)
scatter_plot = plt.figure()
axesl = scatter_plot.add_subplot(1, 1, 1)
axesl.scatter(tips['total_bill'], tips['tip'])
axesl.set_title('Scatterplot of Total Bill vs Tip')
axesl.set_xlabel('Total Bill')
axesl.set_ylabel('Tip') 
scatter_plot.show()
图片.png
  • 箱形图
boxplot = plt.figure()
axesl = boxplot.add_subplot(1, 1, 1)
axesl.boxplot([tips[tips['sex'] == 'Female']['tip'], tips[tips ['sex'] == 'Male']['tip']])
axesl.set_xlabel('Sex')
axesl.set_ylabel('Tip')
axesl.set_title('Boxplot of Tips by Sex')
图片.png
  • 多变量绘图
# create a color variable based on the sex
def recode_sex(sex):
    if sex == 'Female':
        return 0
    else:
        return 1
    
tips['sex_color'] = tips['sex'].apply(recode_sex)
scatter_plot = plt.figure()
axesl = scatter_plot.add_subplot(1, 1, 1)
axesl.scatter(x=tips['total_bill'], y=tips['tip'], s=tips['size'] * 10,
c=tips['sex_color'], alpha=0.5)
axesl.set_title('Total Bill vs Tip colored by Sex and sized by Size')
axesl.set_xlabel('Total Bill')
axesl.set_ylabel('Tip')
scatter_plot.show()
图片.png

参考资料

相关文章

网友评论

    本文标题:数据分析工具pandas快速入门教程3绘图2matplotlib

    本文链接:https://www.haomeiwen.com/subject/ytupiftx.html