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

标签 python matplotlib seaborn

我想使用 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/

相关文章:

python - 在保留 matplotlib 样式的同时使用 seaborn 缩放图形的字体

python - Python 的颜色 (seaborn) : colors without adding to DataFrame

python - python中的饼图雷达图(披萨片)

Python:有没有办法用 Matplotlib 绘制 "partial"曲面图?

python - Pandas .loc 的列表值抛出 "ValueError: Lengths must match to compare"错误

python - 使用正则表达式来匹配我的字符串内容?

python-3.x - Matplotlib:极坐标轴刻度标签位置

python - Seaborn - 从 DataFrame 直方图中删除间距

Python 多处理性能仅随所用内核数的平方根提高

python - 缺少 1 个必需的仅关键字参数