【问题标题】:Size of bubbles in plotly.express.scatter_mapboxplotly.express.scatter_mapbox 中气泡的大小
【发布时间】:2020-08-05 12:22:26
【问题描述】:

我正在尝试使用 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()

这就是它的样子: open street map with bubbles

如何影响气泡的大小,使较小的值具有较小的直径,较大的值具有较大的直径?

我尝试使用 size_max-value,但所有气泡的大小仍然相同,只是它们都更大或更小。

【问题讨论】:

    标签: python pandas plotly mapbox plotly-express


    【解决方案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()
    

    现在结果看起来更好了:

    applied scale the to the size-parameter

    【讨论】:

      【解决方案2】:

      您的代码是正确的。由于数据与大小的相似性,大小并不明显。我特意将不来梅的数据修改为更大的尺寸来绘制图表。另一个解决方法是更改​​“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=...-参数可以从色标中独立更改。所以通过一点数学,我可以计算出一种规模。我会在一分钟内发布这个解决方案。
      • 这些数字仅用于说明目的,因此没有特殊意图进行更改。
      猜你喜欢
      • 2016-07-31
      • 2011-12-23
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 2016-01-14
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多