如何将一个传奇人物与两个海洋小号相结合?
我想拥有一个非常适合两个子图顶部的传奇(不一定需要跨越剧情的整个宽度,而需要在图之外)。我知道您可以使用bbox_to_anchor()
,但某种程度上似乎无法正常工作。它总是将一个子图移开。
fig, ax = plt.subplots(1, 2)
sns.barplot(x="day", y="total_bill", hue="sex", data=tips, ax = ax[0])
ax[0].legend_.remove()
sns.barplot(x="day", y="total_bill", hue="sex", data=tips, ax = ax[1])
sns.move_legend(ax[1], loc = "center", bbox_to_anchor=(-0.5, 1.1), ncol=2, title=None, frameon=False)
fig.tight_layout()
I would like to have a single legend that nicely fits on top of both the subplots (doesn't necessarily need to span the entire width of the plots, but needs to be outside the plot). I know you can work with bbox_to_anchor()
but somehow this doesn't seem to work nicely. It always moves one subplot away.
fig, ax = plt.subplots(1, 2)
sns.barplot(x="day", y="total_bill", hue="sex", data=tips, ax = ax[0])
ax[0].legend_.remove()
sns.barplot(x="day", y="total_bill", hue="sex", data=tips, ax = ax[1])
sns.move_legend(ax[1], loc = "center", bbox_to_anchor=(-0.5, 1.1), ncol=2, title=None, frameon=False)
fig.tight_layout()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以解决差距。
1:使用
sns.catplot
:这可能需要将数据加倍,尽管如果您要在每个子图中绘制不同的变量,则可以
。
> AutoScale 轴
仍然需要一点
bbox_to_anchor
弹奏,您可能想更改正确的y轴标签(以及ticks/ticklabels)。There are a couple of ways that I would approach closing the gap.
1: Use a
sns.catplot
:This potentially requires doubling your data, though if you're plotting different variables in each subplot you may be able to
melt
your data2:
autoscale
the axesThis still requires a little bit of
bbox_to_anchor
fiddling and you probably want to change the right y-axis label (and ticks/ticklabels).