Python数据可视化编程,Matplotlib、 直方图等你会几种?

Python数据可视化库
- Matplotlib:其能够支持所有的2D作图和部分3D作图。能通过交互环境做出印刷质量的图像。
- Seaborn:基于Matplotlib,seaborn提供许多功能,比如:内置主题、颜色调色板、函数和提供可视化单变量、双变量、线性回归的工具。其能帮助我们构建复杂的可视化。
数据集
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-Matplotlib(3) 条形图实战


好了,今天的知识就分享到这里,欢迎关注爱编程的南风,私信关键词:学习资料,获取更多学习资源,如果文章对你有有帮助,请收藏关注,在今后与你分享更多学习python的文章。同时欢迎在下面评论区留言如何学习python。
相关推荐
syThinkCool 2020-07-16
maybeyoucan 2020-05-17
jiahaohappy 2020-05-11
Tonybo 2020-04-27
wordmhg 2020-07-28
xinxuwang 2019-10-26
meylovezn 2020-09-15
adamlovejw 2020-06-20
woxmh 2020-06-03
laohyx 2020-05-07
liusarazhang 2020-04-10
tengyunjiawucom 2020-03-27
wangdaren 2020-03-27
Leonwey 2020-03-03
xinhao 2020-02-18
Jonderwu 2020-02-10
数据齿轮 2020-01-31
FrederickBala 2020-01-29