pythonseabornwarningspalette

How can I resolve "UserWarning: The palette list has more values (10) than needed (4), which may not be intended"?


I am using "tab10" palette because of its distinct colors blue, green, orange and red.

k_clust = KMeans(n_clusters=4, random_state= 35, n_init=1).fit(df_normal)

palette = sns.color_palette("tab10")
sns.pairplot(new_df, hue="clusters", palette=palette)

The number of clusters are only 4 and the palette "tab10" has more than 4 colors. Is there a way to address this UserWarning?

The output is:

C:\Users\....\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\seaborn\axisgrid.py:1507: UserWarning: The palette list has more values (10) than needed (4), which may not be intended.
  func(x=vector, **plot_kwargs)

Solution

  • The docs for color_palette() say that you can pass n_colors=4 to the call.

    Try this:

    ...
    palette = sns.color_palette("tab10", n_colors=4) # equal to n_clusters
    ...