ITxiaobaibai 2018-06-11

Python数据可视化库
数据集
EMPIDGenderAgeSalesBMIIncomeE001M34123Normal350E002F40114Overweight450E003F37135Obesity169E004M30139Underweight189E005F44117Underweight183E006M36121Normal80E007M32133Obesity166E008F26140Normal120E009M32133Normal75E010M36133Underweight40
作图
# -*- coding:UTF-8 -*-
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
# 0、导入数据集
df = pd.read_excel('first.xlsx', 'Sheet1')
1
2
3
4
5
6
7
8
9
# 1、直方图
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(df['Age'], bins=7)
plt.title('Age distribution')
plt.xlabel('Age')
plt.ylabel('Employee')
plt.show()
1
2
3
4
5
6
7
8
# 2、箱线图 fig = plt.figure() ax = fig.add_subplot(111) ax.boxplot(df['Age']) plt.show() 1 2 3 4 5

# 3、小提琴图 sns.violinplot(df['Age'], df['Gender']) sns.despine() plt.show() 1 2 3 4

# 4、条形图
var = df.groupby('Gender').Sales.sum()
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel('Gender')
ax1.set_ylabel('Sum of Sales')
ax1.set_title('Gender wise Sum of Sales')
var.plot(kind='bar')
plt.show()
1
2
3
4
5
6
7
8
9
# 5、折线图
var = df.groupby('BMI').Sales.sum()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('BMI')
ax.set_ylabel('Sum of Sales')
ax.set_title('BMI wise Sum of Sales')
var.plot(kind='line')
plt.show()
1
2
3
4
5
6
7
8
9
# 6、堆积柱形图 var = df.groupby(['BMI', 'Gender']).Sales.sum() var.unstack().plot(kind='bar', stacked=True, color=['red', 'blue']) plt.show() 1 2 3 4

# 7、散点图 fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(df['Age'], df['Sales']) plt.show() 1 2 3 4 5

# 8、气泡图 fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(df['Age'], df['Sales'], s=df['Income']) # 第三个变量表明根据收入气泡的大小 plt.show()



好了,今天的知识就分享到这里,欢迎关注爱编程的南风,私信关键词:学习资料,获取更多学习资源,如果文章对你有有帮助,请收藏关注,在今后与你分享更多学习python的文章。同时欢迎在下面评论区留言如何学习python。