Python seaborn 在子图中生成单独的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33925494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:12:06  来源:igfitidea点击:

seaborn produces separate figures in subplots

pythonmatplotlibseaborn

提问by lgd

i'm trying to make a 2x1 subplot figure in seaborn using:

我正在尝试使用以下方法在 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()

it produces two separate figures instead of a single figure with two subplots. why does it do this and how can seaborn be called multiple times for separate subplots?

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

i tried looking at the post referenced below but i cannot see how the subplots can be added even if factorplotis called first. can someone show an example of this? it would be helpful. my attempt:

我尝试查看下面引用的帖子,但即使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()

采纳答案by tmdavison

The issue is that factorplotcreates a new FacetGridinstance (which in turn creates its own figure), on which it will apply a plotting function (pointplot by default). So if all you want is the pointplot, it would make sense to just use pointplot, and not factorplot.

问题是factorplot创建一个新FacetGrid实例(反过来创建自己的图形),它将在其上应用绘图函数(默认情况下为 pointplot)。因此,如果您想要的只是pointplot,那么使用pointplot, 而不是factorplot

The following is a bit of a hack, if you reallywant to, for whatever reason, tell factorplotwhich Axesto perform its plotting on. As @mwaskom points out in comments, this is not a supported behaviour, so while it might work now, it may not in the future.

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

You can tell factorplotto plot on a given Axesusing the axkwarg, which gets passed on down to matplotlib, so the linked answer does sort of answer your query. However, it will still create the second figure because of the factorplotcall, but that figure will just be empty. A workaround here it to close that extra figure before calling plt.show()

您可以告诉使用kwargfactorplot绘制给定Axesax图,该 kwarg 被传递到matplotlib,因此链接的答案确实可以回答您的查询。但是,由于factorplot调用,它仍将创建第二个图形,但该图形将是空的。一种解决方法是在调用之前关闭额外的数字plt.show()

For example:

例如:

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()

enter image description here

在此处输入图片说明