您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

问题是factorplot创建了一个FacetGrid实例(后者又创建了自己的图形),并在其上应用了绘图功能认情况下为点图)。因此,如果您想要的pointplot只是使用pointplot,那么就有意义,而不是factorplot

如果您 出于任何原因要告诉执行factorplot哪个Axes绘图,以下内容都是一个小技巧。正如@mwaskom在评论中指出的那样,这不是受支持的行为,因此尽管它现在可能会起作用,但将来可能会不起作用。

您可以使用kwarg告诉factorplot绘制给定Axesax图形,该kwarg传递给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()

在此处输入图片说明

其他 2022/1/1 18:41:49 有447人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶