Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly-express figure color legend should display only integer values

A graph created with plotly express using a choropleth map shows distribution of count values associated to the regions of Italy:

import pandas as pd
import requests
import plotly.express as px

data = {'regions' : ['Piemonte', 'Trentino-Alto Adige', 'Lombardia', 'Puglia', 'Basilicata', 
           'Friuli Venezia Giulia', 'Liguria', "Valle d'Aosta", 'Emilia-Romagna',
           'Molise', 'Lazio', 'Veneto', 'Sardegna', 'Sicilia', 'Abruzzo',
           'Calabria', 'Toscana', 'Umbria', 'Campania', 'Marche'],
        'counts' : [3, 3, 4, 2, 0,
                    4, 4, 0, 0,
                    1, 1, 0, 3, 4, 4,
                    3, 4, 3, 2, 4]}

df = pd.DataFrame.from_dict(data)

# Read the geojson data with Italy's regional borders [enter image description here][2]from github
repo_url = 'https://gist.githubusercontent.com/datajournalism-it/48e29e7c87dca7eb1d29/raw/2636aeef92ba0770a073424853f37690064eb0ea/regioni.geojson'
italy_regions_geo = requests.get(repo_url).json()

fig = px.choropleth(data_frame=df, 
                    geojson=italy_regions_geo, 
                    locations='regions', # name of dataframe column matching the region names
                    featureidkey='properties.NOME_REG',  # path to field in GeoJSON feature object with which to match the values passed in to locations
                    color='counts',
                    color_continuous_scale="Magma",
                    scope="europe",
                   )
fig.update_geos(resolution=50, 
                showcountries=False, 
                showcoastlines=False, 
                fitbounds="locations",
                showland=False)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
fig.write_image("./italy.png") 

when the maximum value of count is four, the figure color legend will include labels with decimals, which is not needed, given that values should be treated as integers. Decimals disappear as soon as the maximum value of count is 5, or larger. Map of Italy realised with plotly-express choroplet How do I control the appearance of decimals in the figure color legend?

like image 846
massimopinto Avatar asked Dec 18 '25 19:12

massimopinto


1 Answers

  • it's documented here https://plotly.com/python/reference/layout/coloraxis/#layout-coloraxis-colorbar-dtick
  • hence one additional layout instruction below
import pandas as pd
import requests
import plotly.express as px

data = {'regions' : ['Piemonte', 'Trentino-Alto Adige', 'Lombardia', 'Puglia', 'Basilicata', 
           'Friuli Venezia Giulia', 'Liguria', "Valle d'Aosta", 'Emilia-Romagna',
           'Molise', 'Lazio', 'Veneto', 'Sardegna', 'Sicilia', 'Abruzzo',
           'Calabria', 'Toscana', 'Umbria', 'Campania', 'Marche'],
        'counts' : [3, 3, 4, 2, 0,
                    4, 4, 0, 0,
                    1, 1, 0, 3, 4, 4,
                    3, 4, 3, 2, 4]}

df = pd.DataFrame.from_dict(data)

# Read the geojson data with Italy's regional borders [enter image description here][2]from github
repo_url = 'https://gist.githubusercontent.com/datajournalism-it/48e29e7c87dca7eb1d29/raw/2636aeef92ba0770a073424853f37690064eb0ea/regioni.geojson'
italy_regions_geo = requests.get(repo_url).json()

fig = px.choropleth(data_frame=df, 
                    geojson=italy_regions_geo, 
                    locations='regions', # name of dataframe column matching the region names
                    featureidkey='properties.NOME_REG',  # path to field in GeoJSON feature object with which to match the values passed in to locations
                    color='counts',
                    color_continuous_scale="Magma",
                    scope="europe",
                   )
fig.update_geos(resolution=50, 
                showcountries=False, 
                showcoastlines=False, 
                fitbounds="locations",
                showland=False)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(coloraxis={"colorbar":{"dtick":1}})
fig.show()
like image 75
Rob Raymond Avatar answered Dec 20 '25 09:12

Rob Raymond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!