【问题标题】:ValueError: c argument has n elements, which is not acceptable for use with x with size 0, y with size 0ValueError: c 参数有 n 个元素,这不能与大小为 0 的 x 和大小为 0 的 y 一起使用
【发布时间】:2020-07-19 20:10:06
【问题描述】:

我正在尝试使用 matplotlib/seaborn 绘制散点图:

plt.figure(figsize=(16,10))
sns.scatterplot(
    x=[i[0] for i in tsne_data],
    y=[i[1] for i in tsne_data],
    alpha=0.3,
    color=label_colors
)

我收到了错误:

ValueError: 'c' argument has 267794 elements, which is not acceptable for use with 'x' with size 0, 'y' with size 0.

我的数据:

  • label_colors — 一个包含 267,794 个元素的 Python 列表。每个元素都是一个字符串,例如“红色”、“蓝色”、“紫色”。
  • tsne_data — 具有 267,794 个元素的 Numpy 二维数组。每个元素都是一个 x,y 坐标,例如[ 9.417695 , -25.48891 ]。形状为 (267794, 2)。

我不明白为什么会出现此错误,尤其是为什么“x”和“y”未被识别为具有任何长度。我已经尝试使用 pandas 数据框来代替,其中我有一个“x”和“y”列,然后设置 x=df['x']x=list(df['x']) 但我得到了同样的错误。如何绘制我的tsne_data,以便它的 267,794 个点中的每一个都由label_colors 中相应索引处指定的颜色着色?

【问题讨论】:

    标签: python matplotlib seaborn valueerror


    【解决方案1】:

    根据seaborn documentation,您应该使用参数hue,例如:

    plt.figure(figsize=(16,10))
    sns.scatterplot(
        x=[i[0] for i in tsne_data],
        y=[i[1] for i in tsne_data],
        alpha=0.3,
        hue=label_colors
    )
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2019-11-28
      • 2013-12-25
      • 2023-03-03
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多