Search code examples
pythonmatplotlibseabornvalueerror

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


I am trying to plot a scatterplot using 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
)

I'm getting the error:

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

My data:

  • label_colors — a Python list with 267,794 elements. Each element is a string, e.g. 'red', 'blue', 'purple'.
  • tsne_data — a Numpy 2D array with 267,794 elements. Each element is a x, y coordinate, e.g. [ 9.417695 , -25.48891 ]. Shaped as (267794, 2).

I can't understand why I'm getting this error, especially why 'x' and 'y' are not recognized as having any length. I have attempted this with a pandas dataframe instead, where I have an 'x' and 'y' column, then setting x=df['x'] or x=list(df['x']) but I get the same error. How can I plot my tsne_data so that each of its 267,794 point is colored by the color specified at its corresponding index in label_colors?


Solution

  • As per the seaborn documentation you should use the param hue for example:

    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
    )