seaborn在子图中生成单独的图形 [英] seaborn produces separate figures in subplots

查看:35
本文介绍了seaborn在子图中生成单独的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法在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", 数据=数据)plt.errorbar(np.arange(len(data["x"])), data["y"], yerr=data["s"])plt.subplot(2, 1, 2)sns.factorplot(x="x", y="y", 数据=数据)plt.show()

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

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

data = pandas.DataFrame({"x": [1, 2, 4],"y": [10,20,40],"s":[0.01,0.1,1.0]})无花果= plt.figure()sns.pointplot(x ="x",y ="y",data = data)ax = sns.factorplot(x="x", y="y", 数据=数据)fig.add_subplot(212,轴=轴)plt.errorbar(np.arange(len(data ["x"])),data ["y"],yerr = data ["s"])plt.show()

解决方案

问题在于 factorplot 创建了一个新的 FacetGrid 实例(进而创建了自己的图形),它将在其上应用绘图功能(默认情况下为点图).因此,如果您只想要 pointplot,那么只使用 pointplot 而不是 factorplot 是有意义的.

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

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

之前关闭额外的数字

例如:

 将matplotlib.pyplot导入为plt进口大熊猫将 seaborn 作为 sns 导入将numpy导入为np数据= pandas.DataFrame({"x":[1、2、4],"y":[10,20,40],"s":[10,10,10]})#我增加了您的错误,以便可以看到它们#创建一个图形实例,然后创建两个子图无花果= plt.figure()ax1 = fig.add_subplot(211)ax2 = fig.add_subplot(212)#告诉pointplot使用ax参数在ax1上绘图sns.pointplot(x ="x",y ="y",data = data,ax = ax1)#将误差线直接绘制在ax1上ax1.errorbar(np.arange(len(data["x"])), data["y"], yerr=data["s"])# 告诉因子图使用 ax 参数在 ax2 上绘图#还将FacetGrid存储在'g'中g=sns.factorplot(x="x", y="y", 数据=数据, ax=ax2)# 关闭我们不需要的 FacetGrid 图 (g.fig)plt.close(g.fig)plt.show()

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

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?

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

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

解决方案

The issue is that factorplot creates a new FacetGrid instance (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.

The following is a bit of a hack, if you really want to, for whatever reason, tell factorplot which Axes to 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.

You can tell factorplot to plot on a given Axes using the ax kwarg, 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 factorplot call, but that figure will just be empty. A workaround here it to close that extra figure before calling 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()

这篇关于seaborn在子图中生成单独的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆