Python plotly.express.scatter\u映射框中气泡的大小
我正在尝试使用plotly.express创建地图,该地图显示的值为气泡(圆) 目前的数值范围为16000到21500。我已经把所有的东西都安装好并运行了,气泡以不同的颜色显示,但是,它们或多或少都是相同大小的 我想要的是一个小气泡显示的最小值,一个大气泡显示的最大值,以及介于两者之间的其他值 这就是我的数据框的外观:Python plotly.express.scatter\u映射框中气泡的大小,python,pandas,plotly,mapbox,plotly-express,Python,Pandas,Plotly,Mapbox,Plotly Express,我正在尝试使用plotly.express创建地图,该地图显示的值为气泡(圆) 目前的数值范围为16000到21500。我已经把所有的东西都安装好并运行了,气泡以不同的颜色显示,但是,它们或多或少都是相同大小的 我想要的是一个小气泡显示的最小值,一个大气泡显示的最大值,以及介于两者之间的其他值 这就是我的数据框的外观: country average long lat 0 Baden-Württemberg
country average long lat
0 Baden-Württemberg 19166.381092 9.179330 48.781956
1 Bayern 18786.556728 11.572199 48.137859
2 Berlin 21463.044514 13.387224 52.533707
3 Brandenburg 19622.567766 13.070526 52.405476
4 Bremen 16197.013903 8.805129 53.081386
5 Hamburg 18426.436184 10.001104 53.554158
这就是我展示它的方式:
fig = px.scatter_mapbox(all_data, lat="lat", lon="long", hover_name="country", hover_data=["country", "average"],
color="average",
size="average", color_continuous_scale=px.colors.sequential.matter, size_max=20,
zoom=5, height=1000, mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
这就是它的样子:
如何影响气泡的大小,使较小的值具有较小的直径,而较大的值具有较大的直径
我试着使用size\u max-value,但所有气泡的大小仍然相同,只是它们都较大或较小。您的代码是正确的。由于数据主题与大小相似,因此大小不明显。我故意将不莱梅的数据修改为更大的尺寸来绘制图表。另一个修复方法是更改“size_max”。我还将气泡的颜色更改为不与贴图混合的颜色
import pandas as pd
import numpy as np
import io
data = '''
country average long lat
0 Baden-Württemberg 19166.381092 9.179330 48.781956
1 Bayern 18786.556728 11.572199 48.137859
2 Berlin 21463.044514 13.387224 52.533707
3 Brandenburg 19622.567766 13.070526 52.405476
4 Bremen 46197.013903 8.805129 53.081386 # average value update
5 Hamburg 18426.436184 10.001104 53.554158
'''
all_data = pd.read_csv(io.StringIO(data), sep='\s+')
import plotly.express as px
fig = px.scatter_mapbox(all_data, lat="lat", lon="long", hover_name="country", hover_data=["country", "average"],
color="average",
size="average", color_continuous_scale=px.colors.sequential.Rainbow, size_max=40,
zoom=5, height=1000, mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
我发现,size参数可以获取反映比例的值列表。这不会影响地图右侧绘制的比例 所以我这样做了:
# doing a little bit of math here to calculate a scale to reflect the difference between
# the minimum and the maximum of the average prices (could probably be done much more elegant,
# but this does the job)
#
# first, calculate a ratio between max and min and divide it to have 16 steps
all_data_diffq = (all_data["mean"].max() - all_data["mean"].min()) / 16
# calculate the scale value by subtracting the minium value from the average price, divide
# that by the ratio which will give the scale a value between 0...15 and add 1 to it so that
# the scale values start at 1 (to be visible on the map)
# add the according scale to each row
# the scale column will then be used for size=... parameter in the scatter_mapbox call below
all_data["scale"] = (all_data["mean"] - all_data["mean"].min()) / all_data_diffq + 1
我的数据帧现在看起来像这样:
country mean long lat scale
0 Baden-Württemberg 19166.381092 9.179330 48.781956 10.021952
1 Bayern 18786.556728 11.572199 48.137859 8.867916
2 Berlin 21463.044514 13.387224 52.533707 17.000000
3 Brandenburg 19622.567766 13.070526 52.405476 11.408003
4 Bremen 16197.013903 8.805129 53.081386 1.000000
5 Hamburg 18426.436184 10.001104 53.554158 7.773747
现在,对scatter_mapbox()的调用使用“scale”列作为大小参数:
fig = px.scatter_mapbox(all_data, lat="lat", lon="long", hover_name="country", hover_data=["country", "mean"],
color="mean",
size=all_data["scale"], color_continuous_scale=px.colors.sequential.Rainbow,
size_max=50, zoom=5, height=1000, mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
现在结果看起来更好了:
非常感谢。我得出了同样的结论,但是,我不能改变不莱梅的数据,因为这是它的价值所在。但是,我发现,size=…-参数可以从色阶独立地改变。通过一点数学,我可以计算出一种比例。我将在一分钟后发布此解决方案。更改的数字仅用于说明目的,因此没有特别打算更改它们。