[Python] How do I modify a Matplotlib colorbar so the labels are integers? [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I have a GeoDataFrame that I'm plotting in Matplotlib as a choropleth map.

    gdf.plot(ax = ax,
    column = 'column to color by',
    legend = True,
    legend_kwds={"label": "Label", "orientation": "horizontal", 'pad':0},
    cmap = 'viridis')


    This automatically generates a colorbar legend that looks like this:

    [​IMG]

    This is fine, but the data in the "column to color by" are all integers between 1 and 3 (and the column in the GeoDataFrame is an integer dtype). Is there any easy way to modify the legend keywords so that the colorbar ticks only label the integers?

    [​IMG]

    I know there are ways to create discrete colorbars, but I was hoping for a solution to quickly modify the existing code I have.

    Continue reading...

Compartilhe esta Página