2024-10-08 19:19:57 发布
网友
在我的seaborntsplot中,颜色与绘制的线条不匹配:
tsplot
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.图例文档页面,无法思考为什么会发生这种情况。在
tsplot在线条周围添加一些alpha较低的区域。即使它们不可见(因为只绘制了一条直线),它们也会进入图例中。在
解决方法是直接从绘图中获取线条:
h = plt.gca().get_lines() plt.legend(handles=h, labels=item_list)
完整示例:
我只想提一下,在这种情况下似乎没有理由使用tsplot。简单的线条图(plt.plot)就足够了,而且混淆的可能性更小。blow代码生成的输出与上面的完全相同。在
plt.plot
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()
tsplot
在线条周围添加一些alpha较低的区域。即使它们不可见(因为只绘制了一条直线),它们也会进入图例中。在解决方法是直接从绘图中获取线条:
完整示例:
^{pr2}$我只想提一下,在这种情况下似乎没有理由使用
tsplot
。简单的线条图(plt.plot
)就足够了,而且混淆的可能性更小。blow代码生成的输出与上面的完全相同。在相关问题 更多 >
编程相关推荐