plotly-express 图形颜色图例应仅显示整数值
plotly-express figure color legend should display only integer values
使用 choropleth map 使用 plotly express 创建的图表显示了与意大利地区相关的 count
值的分布:
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")
当 count
的最大值为四时,图形颜色图例将包含带有小数点的标签,这是不需要的,因为值应被视为整数。一旦 count
的最大值为 5 或更大,小数点就会消失。
如何控制图形颜色图例中小数点的出现?
- 此处记录https://plotly.com/python/reference/layout/coloraxis/#layout-coloraxis-colorbar-dtick
- 因此下面有一个额外的布局说明
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()
使用 choropleth map 使用 plotly express 创建的图表显示了与意大利地区相关的 count
值的分布:
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")
当 count
的最大值为四时,图形颜色图例将包含带有小数点的标签,这是不需要的,因为值应被视为整数。一旦 count
的最大值为 5 或更大,小数点就会消失。
- 此处记录https://plotly.com/python/reference/layout/coloraxis/#layout-coloraxis-colorbar-dtick
- 因此下面有一个额外的布局说明
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()