How to add thousand separator in geopandas pyplot legend?

Question:

I am plotting a legend of a map with geopandas and matplotlib.pyplot.
I want to see the legend values with a thousand separator, e.g. 100,000,000 at the moment it is shown like ‘100000000’. I would also like to be able to write the number like ‘100mill’. How can I achieve these two options?

df_test.plot(
             ax=ax,
             zorder=1,
             cax=cax,
             column='tax',
             cmap='YlOrRd',
             legend=True,
             legend_kwds={'label': '(USD)',
                          'orientation': "vertical",
                          'pad': 0.3,
                          'format': '%.0f'},
);
Asked By: eetiaro

||

Answers:

It seems there is no option to set a thousand separator with geopandas plot function and legend_kwds.
I solved it by retrieving the automatically generated axis labels with

ax.get_figure().get_axes()[1].get_yticks()

and then creating a custom colorbar:

legend_labels_auto = ax.get_figure().get_axes()[1].get_yticks()
vmin, vmax = legend_labels_auto[0], legend_labels_auto[-1]
sm = plt.cm.ScalarMappable(cmap='YlOrRd', norm=plt.Normalize(vmin=vmin, 
vmax=vmax))
sm._A = []
cbar = fig.colorbar(sm, cax=cax, fraction=0.1, label='test')

In the geodataframe plot one has to add:

geodf.plot{
           ax=ax,
           cax=cax,
           legend=True} # legend=True even though we create our own custom legend later
Answered By: eetiaro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.