问题是factorplot
创建了一个新FacetGrid
实例(后者又创建了自己的图形),并在其上应用了绘图功能(默认情况下为点图)。因此,如果您想要的pointplot
只是使用pointplot
,那么就有意义,而不是factorplot
。
如果您 出于任何原因要告诉执行factorplot
哪个Axes
绘图,以下内容都是一个小技巧。正如@mwaskom在评论中指出的那样,这不是受支持的行为,因此尽管它现在可能会起作用,但将来可能会不起作用。
您可以使用kwarg告诉factorplot
绘制给定Axes
的ax
图形,该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()