seaborn包如何实现同一画布上绘制多个子图

平时用matplotlib画图,使用figure和subplot方法就可以实现同一画布上绘制多个子图。但是使用seaborn包时,尝试同样的方法却画不到同一画布上。

img

img

img

Seaborn 可以使用 FacetGrid 类绘制分面绘图。你可以使用 FacetGrid 类的 map() 方法绘制多个子图。

下面是一个例子,展示了如何使用 FacetGrid 类绘制同一画布上的多个子图:

import seaborn as sns
import matplotlib.pyplot as plt

# 创建一个 FacetGrid 对象
g = sns.FacetGrid(data, col="col1", row="col2")

# 使用 map() 方法绘制多个子图
g.map(sns.lineplot, "x", "y")

plt.show()

在这个例子中,data 是一个 DataFrame,col1 和 col2 是列名,x 和 y 是要绘制在子图上的变量。FacetGrid 对象会自动根据 col1 和 col2 的值绘制多个子图。