U, inv = np.unique(c, return_inverse=True)įor cat, color in zip(u, plt.cm.viridis(np.linspace(0,1,len(u)))):Īx.scatter(x, y, color=color, label=cat)Īx.plot(x, y, color=color, ls="", marker="o", label=cat)Ĭmap, norm = from_levels_and_colors(np.arange(0,len(u) 1)-0.5, plt.cm.viridis(np. Is it possible to provide a list of classes (instead of colors) and then pass a dict like mapping with class -> color?įrom lors import from_levels_and_colorsĬ = np.random.randint(0, n_cats, n).astype(str) What I would like to do is create a scatter plot with custom colors for every class, with a corretly labeled legend and not use loops and the label argument. What would be helpful for me is that the list of lines is returned so i can map the labels by myself.īut maybe I am just using scatter incorretly in this instace. can you tell what exactly you would expect the legend to show in the above example case? However, this seems to not support generating a legend without specifically plotting each group separately. Possibly we might think about extending the functionality, but for this it would be good to collect use cases. pyplot.scatter allows for passing to c an array that corresponds to groups, which will then color the points based on those groups. The reason is that in case you specify the scatter colors as a simple list of colors, no mapping is happening, and therefore we cannot know what to show in the legend (other than maybe the color itself?). In what other manner could I use the c argument then? I found this warning not helpful because I was using the same parameter in both examples Make sure to specify the values to be colormapped via the c argument. \matplotlib\collections.py:960: UserWarning: Collection without array used. That is expected, and there is a warning about it. my example was slightly different before posting. I guess you meant to say that the legend_elements of scatter1 is empty, right? Matplotlib backend ( print(matplotlib.get_backend())): 'module://_inline'.The first legend does not contain anything, since the legend_elements of scatter1 is empty. legend_elements(), loc = "lower right", title = "Classes2") legend_elements(), loc = "upper left", title = "Classes1")
0 Comments
Leave a Reply. |