工具癖数据分析工具pandas快速入门教程

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

2018-08-23  本文已影响7人  python测试开发

matplotlib统计图

seaborn的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

参考资料

上一篇 下一篇

猜你喜欢

热点阅读