seaborn tsplot:传奇色彩褪色

2024-10-08 19:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的seaborntsplot中,颜色与绘制的线条不匹配:

Colors don't match the lines drawn.

for item in item_list:
    sns.tsplot(get_data(), color=get_color(), legend=True)

sns.plt.legend(labels=item_list)
sns.plt.show()

我读了sns.ts绘图以及plt.图例文档页面,无法思考为什么会发生这种情况。在


Tags: infordataget颜色绘制pltitem
1条回答
网友
1楼 · 发布于 2024-10-08 19:19:57

tsplot在线条周围添加一些alpha较低的区域。即使它们不可见(因为只绘制了一条直线),它们也会进入图例中。在

解决方法是直接从绘图中获取线条:

h = plt.gca().get_lines()
plt.legend(handles=h, labels=item_list)

完整示例:

^{pr2}$

enter image description here

我只想提一下,在这种情况下似乎没有理由使用tsplot。简单的线条图(plt.plot)就足够了,而且混淆的可能性更小。blow代码生成的输出与上面的完全相同。在

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

item_list = list("ABXY")

get_data = lambda : np.random.rand(10)
get_color = lambda : "#" + "".join(np.random.choice(list("02468acef"), size=6))

for item in item_list:
    plt.plot(get_data(), color=get_color(), label=item)

plt.legend()
plt.show()

相关问题 更多 >

    热门问题