如何将一个传奇人物与两个海洋小号相结合?

发布于 2025-01-31 10:41:10 字数 619 浏览 2 评论 0原文

我想拥有一个非常适合两个子图顶部的传奇(不一定需要跨越剧情的整个宽度,而需要在图之外)。我知道您可以使用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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2025-02-07 10:41:10

有几种方法可以解决差距。

1:使用sns.catplot

这可能需要将数据加倍,尽管如果您要在每个子图中绘制不同的变量,则可以

import pandas as pd
import seaborn as sns

# Load the dataset twice
tips_a = sns.load_dataset("tips")
tips_b = sns.load_dataset("tips")
# Add a dummy facet variable
tips_a["col"] = "A"
tips_b["col"] = "B"

# Concat them
tips = pd.concat([tips_a, tips_b])

# Use the dummy variable for the `col` param
g = sns.catplot(x="day", y="total_bill", hue="sex", data=tips, kind="bar", col="col")
# Remove the titles and move the legend
g.set_titles("")
sns.move_legend(g, loc="upper center", ncol=2, title=None, frameon=False)

​。


​> AutoScale 轴

仍然需要一点bbox_to_anchor弹奏,您可能想更改正确的y轴标签(以及ticks/ticklabels)。

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(1, 2, figsize=(7, 4))
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="upper center",
    bbox_to_anchor=(-0.1, 1.1),
    ncol=2,
    title=None,
    frameon=False,
)

ax[0].autoscale()
ax[1].autoscale()

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 data

import pandas as pd
import seaborn as sns

# Load the dataset twice
tips_a = sns.load_dataset("tips")
tips_b = sns.load_dataset("tips")
# Add a dummy facet variable
tips_a["col"] = "A"
tips_b["col"] = "B"

# Concat them
tips = pd.concat([tips_a, tips_b])

# Use the dummy variable for the `col` param
g = sns.catplot(x="day", y="total_bill", hue="sex", data=tips, kind="bar", col="col")
# Remove the titles and move the legend
g.set_titles("")
sns.move_legend(g, loc="upper center", ncol=2, title=None, frameon=False)

enter image description here


2: autoscale the axes

This still requires a little bit of bbox_to_anchor fiddling and you probably want to change the right y-axis label (and ticks/ticklabels).

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(1, 2, figsize=(7, 4))
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="upper center",
    bbox_to_anchor=(-0.1, 1.1),
    ncol=2,
    title=None,
    frameon=False,
)

ax[0].autoscale()
ax[1].autoscale()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文