python数据可视化方法

本文深入介绍了数据可视化的五种核心方法:折线图用于比较数据趋势;散点图揭示变量间的关系;饼图展示数据占比;直方图呈现数据分布;箱线图分析数据差异性和异常值。并提供了matplotlib和seaborn两种常用绘图库的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可视化图形有哪些?

比较

比较数据间各类别的关系,比如折线图

联系

查看多个变量之间的关系,比如散点图

构成

数据占整体的比重,比如饼图

分布

变量的分布情况,比如直方图

种类介绍

散点图

我们使用matplotlib包这里,先导入

import matplotlib.pyplot as plt

散点图适用于观察变量之间的关系
散点图语句:

plt.scatter(x, y, marker=None)

x,y是坐标,marker代表了图形中标记的符号形状,有三种:‘x’,‘>’,‘o’。


当然,也可以使用Saborn进行散点图的绘制。
导包路径:

import seaborn as sns

语句:

sns.jointplot(x, y, data=None,kind=‘scatter’)

其中x,y是data中的下标,data是我们要传入的数据,一般是DataFrame类型。kind这类我们取值scatter代表散点的意思。不同的kind能绘制出不同的图形。
我们随机1000个点来画图
例子:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 数据准备
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
# 用 Matplotlib 画散点图
plt.scatter(x, y,marker='x')
plt.show()
# 用 Seaborn 画散点图
df = pd.DataFrame({'x': x, 'y': y})
sns.jointplot(x="x", y="y", data=df, kind='scatter');
plt.show()

结果(摘自极客时间):
matplotlib:
在这里插入图片描述
Seaborn:
在这里插入图片描述

折线图

matplotlib语句:

plt.plot()

我们需要将数据按照x轴的大小进行排序,要不然画出来的折线就无法按照x轴递增的顺序展示。
Seaborn语句:

sns.lineplot (x, y, data=None)

其中x,y是data中的下标,data是我们要传入的数据,一般是DataFrame类型。
例子(摘自极客时间):
设置x,y的数组,x代表时间(年),y数组我们随便取值。

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 数据准备
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [5, 3, 6, 20, 17, 16, 19, 30, 32, 35]
# 使用 Matplotlib 画折线图
plt.plot(x, y)
plt.show()
# 使用 Seaborn 画折线图
df = pd.DataFrame({'x': x, 'y': y})
sns.lineplot(x="x", y="y", data=df)
plt.show()

matplotlib:
在这里插入图片描述
Seaborn:
在这里插入图片描述
可以看到seaborn只是多标记了x,y轴含义

直方图

matplotlib语句:

plt.hist(x,bins=10)

x是一维数组,bins代表直方图中矩形条的数量,默认是10

Seaborn语句:

sns.distplot(x,bins=10,kde=True)

x是一维数组,bins代表直方图中矩形条的数量,kde代表显示核密度估计,默认是True。核密度估计是通过核函数帮助我们来估计概率密度的方法。
例子:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 数据准备
a = np.random.randn(100)
s = pd.Series(a) 
# 用 Matplotlib 画直方图
plt.hist(s)
plt.show()
# 用 Seaborn 画直方图
sns.distplot(s, kde=False)
plt.show()
sns.distplot(s, kde=True)
plt.show()

matplotlib:
在这里插入图片描述
seaborn:
在这里插入图片描述

条形图

matplotlib语句:

plt.bar(x,height)

x代表x轴的位置序列,height是y轴的数值序列,也就是柱子的高度。

seaborn语句:

sns,barplot(x=None,y=None,data=None)

data是DataFrame类型,x,y是data中的变量
例子:

import matplotlib.pyplot as plt
import seaborn as sns
# 数据准备
x = ['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5']
y = [5, 4, 8, 12, 7]
# 用 Matplotlib 画条形图
plt.bar(x, y)
plt.show()
# 用 Seaborn 画条形图
sns.barplot(x, y)
plt.show()

matplotlib:
在这里插入图片描述
seaborn:
在这里插入图片描述

箱线图

箱线图,又称盒式图。由五个数值点组成:最大值、最小值、中位数、和上下四分位数。箱线图能够帮助我们分析出数据的差异性、离散程度和异常值等。
matplotlib语句:

plt.boxplot(x, labels=None)

x代表要绘制箱线图的数据,labels是缺省值,可以为箱线图添加标签。

seaborn语句:

sns.boxplot(x=None,y=None,data=None)

data为DataFrame类型,x,y是data中的变量
例子:

# 数据准备
# 生成 0-1 之间的 10*4 维度数据
data=np.random.normal(size=(10,4)) 
lables = ['A','B','C','D']
# 用 Matplotlib 画箱线图
plt.boxplot(data,labels=lables)
plt.show()
# 用 Seaborn 画箱线图
df = pd.DataFrame(data, columns=lables)
sns.boxplot(data=df)
plt.show()

matplotlib:
在这里插入图片描述
seaborn:
在这里插入图片描述

饼图

matplotlib语句:

plt.pie(x,labels=None)

x代表要绘制饼图的数据,lables是缺省值,可以为饼图添加标签。

例子:

import matplotlib.pyplot as plt
# 数据准备
nums = [25, 37, 33, 37, 6]
labels = ['High-school','Bachelor','Master','Ph.d', 'Others']
# 用 Matplotlib 画饼图
plt.pie(x = nums, labels=labels)
plt.show()

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值