gpt4 book ai didi

python - 在seaborn tsplot中将图例移到图外

转载 作者:行者123 更新时间:2023-12-02 01:38:59 28 4
gpt4 key购买 nike

我想使用 seaborn.tsplot 创建一个时间序列图喜欢在 this example from tsplot documentation ,但随着图例移动到右侧,在图形之外。

example

基于 seaborn's timeseries.py 中的第 339-340 行,看起来像 seaborn.tsplot目前不允许直接控制图例位置:

    if legend:
ax.legend(loc=0, title=legend_name)

是否有 matplotlib 解决方法?
我正在使用 seaborn 0.6-dev。

最佳答案

确实,seaborn到目前为止还不能很好地处理传说。您可以使用 plt.legend()直接通过 matplotlib 控制图例属性,按照Matplotlib Legend Guide .

请注意,在 Seaborn 0.10.0 tsplot 中被删除,您可以使用 lineplot 复制(如果您愿意,可以使用不同的估计值)绘图而不是 tsplot .

片段

import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="darkgrid")

# Load the long-form example gammas dataset
gammas = sns.load_dataset("gammas")

# Plot the response with standard error
sns.lineplot(data=gammas, x="timepoint", y="BOLD signal", hue="ROI")

# Put the legend out of the figure
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

输出

Modified legend position

关于python - 在seaborn tsplot中将图例移到图外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30490740/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com