数据图形化和 Matplotlib
简介
Matplotlib 是一个绘图库,用于创建各种数据可视化图表。它功能强大,灵活性高,能够生成从简单到复杂的静态、交互式和动画式的图形。
Matplotlib 支持多种图表类型,比如:折线图(Line plot)、柱状图(Bar plot)、热图(Heatmap)等等。它可以自定义图表的标题、坐标轴标签、图例、网格、颜色、线型、标记样式等。还可以灵活调整图形的大小、布局和样式。
此外,Seaborn 也是常用的数据图形化库,它基于 Matplotlib 和 Pandas,提供了更加简单、优雅且美观的绘图功能,能够快速生成专业级别的统计图。
可以通过以下命令安装这几个库:
pip install matplotlib, seaborn
在程序中导入功能:
import seaborn as sns
import matplotlib.pyplot as plt
数据集
在图形化显示数据之前,我们需要准备一些数据。在人工智能领域,几个经典的小数据集广泛用于教学示例,包括鸢尾花数据集、波士顿房价数据集和泰坦尼克号乘客数据集。我们也使用这些数据集演示如何使用 Matplotlib 库。
首先,我们需要在网上下载这些数据集及其说明。这些数据通常以 CSV 格式存在,每一行代表一个数据项,例如一朵鸢尾花、一座房子或一位乘客。每个数据集包含不同的列,代表各种特征,例如花瓣长度、房子面积、乘客年龄等。
以下是几个常用的数据下载地址。如果无法访问,请重新搜索可用的网页:
有了下载地址,我们就可以使用 Pandas 库将数据加载到程序中。下面是装载数据集的示例代码:
import pandas as pd
# 鸢尾花数据集 URL
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
# 鸢尾花列名
columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
# 加载鸢尾花数据集
iris = pd.read_csv(url, header=None, names=columns)
print(iris.head())
# 泰坦尼克乘客信息数据集
titanic_url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
titanic = pd.read_csv(titanic_url)
print(titanic.head())
Python 的一些机器学习的库,已经打包了这些小数据集,如果安装这些库,会更简化数据集的使用。比如使用 Scikit-learn 库加载鸢尾花数据集:
pip install scikit-learn
from sklearn.datasets import load_iris
import pandas as pd
# 加载数据
iris = load_iris()
# 转换为 DataFrame
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target
df['species_name'] = df['species'].apply(lambda x: iris.target_names[x])
# 查看数据
print(df.head())
下文所有示例程序,都使用这里得到的鸢尾花 df。
常用图表
散点图(Scatter plot)
散点图用于显示两个变量之间的关系。我们选择 花瓣长度(petal length) 和 花瓣宽度(petal width),用点图展示不同类别的分布。
使用 Matplotlib 原生绘图:
import matplotlib.pyplot as plt
# 设置颜色
colors = ['red', 'green', 'blue']
species = df['species_name'].unique()
# 绘制散点图
plt.figure(figsize=(8, 6))
for i, specie in enumerate(species):
subset = df[df['species_name'] == specie]
plt.scatter(subset['petal length (cm)'], subset['petal width (cm)'],
label=specie, color=colors[i])
# 添加标题和标签
plt.title("Iris Dataset - Petal Length vs Width")
plt.xlabel("Petal Length (cm)")
plt.ylabel("Petal Width (cm)")
plt.legend()
plt.show()
使用 Seaborn 绘图(代码通常更简洁):
import matplotlib.pyplot as plt
import seaborn as sns
# 使用 seaborn 绘制散点图
plt.figure(figsize=(8, 6))
sns.scatterplot(data=df, x='petal length (cm)', y='petal width (cm)', hue='species_name', palette='deep')
plt.title("Iris Dataset - Petal Length vs Width")
plt.show()
折线图(Line plot)
折线图通常用于显示数据随时间变化的趋势。虽然鸢尾花数据集没有时间维度,但我们可以通过对数据进行排序,来观察某种属性的变化趋势。下面的代码展示了不同种类的花瓣长度变化曲线。
import matplotlib.pyplot as plt
import seaborn as sns
# 为了展示效果,我们先对数据按花瓣长度进行排序
df_sorted = df.sort_values(by='petal length (cm)').reset_index()
plt.figure(figsize=(10, 6))
# 绘制折线图
sns.lineplot(data=df_sorted, x=df_sorted.index, y='petal length (cm)', hue='species_name')
plt.title("Petal Length Trend by Species")
plt.xlabel("Sample Index (Sorted)")
plt.ylabel("Petal Length (cm)")
plt.show()
柱状图(Bar plot)
柱状图用于比较不同类别的数值。例如,我们可以计算每种鸢尾花的平均花瓣长度,并用柱状图进行对比。
# 计算每种花的平均花瓣长度
avg_data = df.groupby('species_name')['petal length (cm)'].mean().reset_index()
plt.figure(figsize=(8, 6))
# 使用 Seaborn 绘制柱状图
sns.barplot(data=avg_data, x='species_name', y='petal length (cm)', palette='viridis')
plt.title("Average Petal Length per Species")
plt.ylabel("Average Length (cm)")
plt.show()
饼图(Pie chart)
饼图用于显示各部分占整体的比例。虽然 Seaborn 不直接支持饼图,但 Matplotlib 提供了 pie 函数。我们可以查看数据集中不同种类样本的数量比例(在鸢尾花数据集中,三类样本是均匀分布的,各占 50 个)。
# 统计每种花的数量
species_counts = df['species_name'].value_counts()
plt.figure(figsize=(6, 6))
plt.pie(species_counts, labels=species_counts.index, autopct='%1.1f%%', colors=['#ff9999','#66b3ff','#99ff99'])
plt.title("Distribution of Species")
plt.show()
直方图(Histogram)
直方图用于展示单个变量的数据分布情况(例如数据集中在哪一个区间)。我们 可以看看花萼长度(sepal length)的分布情况。
plt.figure(figsize=(8, 6))
# kde=True 表示同时绘制核密度估计曲线(一条平滑的曲线)
sns.histplot(data=df, x='sepal length (cm)', kde=True, hue='species_name', element="step")
plt.title("Distribution of Sepal Length")
plt.show()