![]() ![]() If not possible, then the only way I can think about is to get each color from the cmap via the following way. Next, we can assign the plots title with plt.title, and then we can invoke. Here’s the minimal example: import matplotlib.pyplot as plt plt. Before plt.show (), call plt.legend () your plot will be displayed with a legend. The colors are from a sequential cmap, so this is why I asked for this feature and need to work backwards. With plt.xlabel and plt.ylabel, we can assign labels to those respective axis. How to add a legend in Python’s Matplotlib library Label it with the label keyword argument in your plot method. legendelements (), loc 'lower left', title 'Classes') ax. I can get the labels via ax.get_legend().get_texts() but I can’t get the colors. scatter (x, y, c c, s s) produce a legend with the unique colors from the scatter legend1 ax. My understanding is that if you want to change the legend you have to define colors and labels. In legend(), we specify title and handles by extracting legend elements from the plot. Y, labeli, colorplt.cm.jet(np.float(i) / len(np.unique(label)))) for i in kmeans.clustercenters: plt.scatter(i0, i1, colorblack, marker . We can try to add legend to the scatterplot colored by a variable, by using legend() function in Matplotlib. ax.legend().legendHandles.get_facecolor() returns an empty list. Add Color to Scatterplot by variable in Matplotlib. I’d like to change the legend marker to squares or rectangles. Gdf.plot(column='pop_est', legend=True, scheme='Quantiles', k=5, Gdf = gpd.read_file(_path('naturalearth_lowres')) I’m using a pkg called geopandas and here’s an example: import geopandas as gpd ![]() So this is why I asked for terms of _facecolor(), sorry for the confusion. I have no control of the legend being plotted. Indeed, as I’ve mentioned a few times before, I need to get the color after the figure being plotted. Sc = ax.Than trying to work backwards from the legend 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.linspace(0,1,len(u)))) 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? Possibly we might think about extending the functionality, but for this it would be good to collect use cases. scatter strategy 1 scatter1 plt.figure () scatter1 plt.scatter ( xx, yy, ccolors, asma, labellabels, ) handles, labels scatter1.legendelements (numlist (np.unique (labels))) plt.legend (handles, labels, loc'lower right') plt.show () just to discover that handles and labels are empty lists. 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 I think the most elegant way is that suggesyted by. E.g.: import matplotlib.pyplot ( 1,2,3, 4,5,6,color 'red','green','blue') When you have a list of lists and you want them colored per list. Make sure to specify the values to be colormapped via the c argument. The normal way to plot plots with points in different colors in matplotlib is to pass a list of colors as a parameter. \matplotlib\collections.py:960: UserWarning: Collection without array used. That is expected, and there is a warning about it. The following also demonstrates how transparency of the markers can be adjusted by giving alpha a value between 0 and 1. my example was slightly different before posting. Scatter plots with a legend To create a scatter plot with a legend one may use a loop and create one scatter plot per item to appear in the legend and set the label accordingly. I guess you meant to say that the legend_elements of scatter1 is empty, right?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |