数据可视化是数据分析的重要环节,它可以将数据以图形化的形式呈现,帮助我们更直观地理解和分析数据。以下是Python中常用的数据可视化库和基本用法。

常用库

  • Matplotlib: Python中最常用的数据可视化库之一。
  • Seaborn: 基于Matplotlib构建,提供更多高级的数据可视化功能。
  • Pandas: 用于数据清洗和转换,同时支持与Matplotlib和Seaborn的集成。

基本用法

1. Matplotlib

首先,我们需要安装Matplotlib库:

pip install matplotlib

然后,使用以下代码绘制一个简单的折线图:

import matplotlib.pyplot as plt

# 数据
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

# 创建图表
plt.plot(x, y)

# 显示图表
plt.show()

Matplotlib 示例

2. Seaborn

Seaborn依赖于Matplotlib,因此需要先安装Matplotlib。

安装完成后,使用以下代码绘制一个散点图:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# 数据
np.random.seed(10)
x = np.random.randn(100)
y = np.random.randn(100)

# 创建图表
sns.scatterplot(x, y)

# 显示图表
plt.show()

Seaborn 示例

扩展阅读