gpt4 book ai didi

python - 没有传说 Seaborn 线图

转载 作者:行者123 更新时间:2023-12-04 11:59:36 26 4
gpt4 key购买 nike

当我使用 Seaborn 在同一轴上绘制多个线图时,不会创建任何图例。即使我在 sns.lineplot 中为图例提供参数“brief”或“full”,也不会显示任何内容,并且调用 ax.get_legend_handles_labels() 会返回两个空列表。

如何在右侧的框中添加图例,将线条的颜色链接到名称?

enter image description here

import seaborn as sns
import matplotlib.pyplot as plt
import random

fig1 = plt.figure(figsize=(12, 12))
ax = fig1.add_subplot(1, 1, 1)

x = range(10)

series = list()
for i in range(3):
y_i = list()
for j in range(10):
y_i.append(random.randint(0, 50))

sns.lineplot(x=x, y=y_i, ax=ax, legend='brief')

plt.show()

最佳答案

那是因为您将简单列表应用为 x 和 y 数据。它们没有名称或标签等元数据。您可以通过 label 添加标签-kwarg 不过,例如:

sns.lineplot(x=x, y=y_i, ax=ax, legend='brief', label=str(i))

或者您决定为您的数据使用 Pandas 系列对象或数据框,它们能够提供列名。

关于python - 没有传说 Seaborn 线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916061/

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