发布于2019-09-11 19:39 阅读(875) 评论(0) 点赞(13) 收藏(1)
我正试图在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?
我试着查看下面引用的帖子,但我看不出即使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
,那么只使用它是有意义的pointplot
,而不是factorplot
。
以下是一个黑客,如果你真的想,无论出于何种原因,告诉factorplot
谁Axes
执行其密谋。正如@mwaskom在评论中指出的那样,这不是受支持的行为,因此虽然它现在可能有用,但未来可能不会。
你可以告诉你使用kwarg factorplot
对给定的情节进行绘图,然后传递给你,因此链接的答案可以回答你的问题。但是,由于调用,它仍然会创建第二个数字,但这个数字只是空的。这里有一个解决方法,它可以在调用前关闭那个额外的数字Axes
ax
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()
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/108043/00138fc1fe7c77ab610e/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!