rather than trying to work backwards from the legend
Indeed, as I’ve mentioned a few times before, I need to get the color after the figure being plotted. I have no control of the legend being plotted. So this is why I asked for it.
In terms of
leg.legendHandles.get_facecolor(), sorry for the confusion. I tried with my use case. It doesn’t seem to work.
I’m using a pkg called
geopandas and here’s an example:
import geopandas as gpd
import matplotlib.pyplot as plt
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
fig, ax = plt.subplots(figsize=(10, 10))
gdf.plot(column='pop_est', legend=True, scheme='Quantiles', k=5,
I’d like to change the legend marker to squares or rectangles.
ax.legend().legendHandles.get_facecolor() returns an empty list.
My understanding is that if you want to change the legend you have to define colors and labels.
I can get the labels via
ax.get_legend().get_texts() but I can’t get the colors.
The colors are from a sequential cmap, so this is why I asked for this feature and need to work backwards.
If not possible, then the only way I can think about is to get each color from the cmap via the following way.
from matplotlib.colors import rgb2hex
from matplotlib.colors import ListedColormap
k = 5
cmap = plt.cm.get_cmap('Blues', k)
cmap_list = ListedColormap([rgb2hex(cmap(i)) for i in range(cmap.N)])