seaborn 在子图中生成单独的图形

seaborn produces separate figures in subplots

我正在尝试使用以下方法在 seaborn 中制作一个 2x1 子图:

data = pandas.DataFrame({"x": [1, 2, 4],
                        "y": [10,20,40],
                        "s": [0.01,0.1,1.0]})

plt.figure()
plt.subplot(2, 1, 1)
sns.pointplot(x="x", y="y", data=data)
plt.errorbar(np.arange(len(data["x"])), data["y"], yerr=data["s"])
plt.subplot(2, 1, 2)
sns.factorplot(x="x", y="y", data=data)
plt.show()

它生成两个单独的图形,而不是一个带有两个子图的图形。为什么要这样做以及如何为单独的子图多次调用 seaborn?

我试着查看下面引用的 post,但即使先调用 factorplot,我也看不到如何添加子图。有人可以举个例子吗?这会很有帮助。我的尝试:

data = pandas.DataFrame({"x": [1, 2, 4],
                        "y": [10,20,40],
                        "s": [0.01,0.1,1.0]})

fig = plt.figure()
sns.pointplot(x="x", y="y", data=data)
ax = sns.factorplot(x="x", y="y", data=data)
fig.add_subplot(212, axes=ax)
plt.errorbar(np.arange(len(data["x"])), data["y"], yerr=data["s"])
plt.show()

问题是 factorplot 创建了一个新的 FacetGrid 实例(它又创建了自己的图形),它将在其上应用绘图函数(默认为点图)。因此,如果您只需要 pointplot,那么只使用 pointplot 而不是 factorplot.

是有意义的

下面是一些 hack,如果你真的想要,不管出于什么原因,告诉factorplot哪个Axes执行它的绘图在。正如@mwaskom 在评论中指出的那样,这不是受支持的行为,因此虽然它现在可能有效,但将来可能无效。

您可以告诉 factorplot 使用 ax kwarg 在给定的 Axes 上绘制,它会向下传递给 matplotlib,因此链接的答案确实排序回答您的查询。但是,由于 factorplot 调用,它仍会创建第二个图形,但该图形只是空的。这里的解决方法是在调用 plt.show()

之前关闭那个额外的数字

例如:

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

data = pandas.DataFrame({"x": [1, 2, 4],
                        "y": [10,20,40],
                        "s": [10,10,10]}) # I increased your errors so I could see them

# Create a figure instance, and the two subplots
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

# Tell pointplot to plot on ax1 with the ax argument
sns.pointplot(x="x", y="y", data=data, ax=ax1)

# Plot the errorbar directly on ax1
ax1.errorbar(np.arange(len(data["x"])), data["y"], yerr=data["s"])

# Tell the factorplot to plot on ax2 with the ax argument
# Also store the FacetGrid in 'g'
g=sns.factorplot(x="x", y="y", data=data, ax=ax2)

# Close the FacetGrid figure which we don't need (g.fig)
plt.close(g.fig)

plt.show()