0

我正在尝试使用 plotly.express 创建一个显示值是气泡(圆圈)的地图。

当前值的范围从 16000 到 21500。我已经启动并运行了所有内容,气泡以不同的颜色显示,但是,它们或多或少都具有相同的大小。

我想要的是用小气泡显示的最小值和用大气泡显示的最大值以及介于两者之间的其他值。

这是我的数据框的样子:

                 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_max-value,但所有气泡仍然具有相同的大小,只是它们都更大或更小。

4

2 回答 2

1

我发现,size-Parameter 可以采用反映比例的值列表。这不会影响在地图右侧绘制的比例。

所以我这样做了:

# 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() 的调用现在使用大小参数的“比例”列:

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()

现在结果看起来更好了:

将缩放应用于尺寸参数

于 2020-08-05T14:13:34.147 回答
0

你的代码是正确的。由于数据与大小的相似性,大小并不明显。我特意将不来梅的数据修改为更大的尺寸来绘制图表。另一个解决方法是更改​​“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()

在此处输入图像描述

于 2020-08-05T12:58:21.667 回答