matplotlib统计图
seaborn的tips数据集包含消费账单的大小,人数,星期几,时间等。
- 加载tips数据集
tips = sns.load_dataset("tips")print(tips.head()) total_bill tip sex smoker day time size0 16.99 1.01 Female No Sun Dinner 21 10.34 1.66 Male No Sun Dinner 32 21.01 3.50 Male No Sun Dinner 33 23.68 3.31 Male No Sun Dinner 24 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 ()
- 散点图(双变量)
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()
- 箱形图
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')
- 多变量绘图
# create a color variable based on the sexdef 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()
参考资料
- 讨论qq群144081101 591302926 567351477 钉钉免费群21745728