【问题标题】:Changing colour range in dropdown更改下拉菜单中的颜色范围
【发布时间】:2021-09-27 18:49:48
【问题描述】:

我正在尝试使用下拉菜单创建一个等值线。我想使用下拉按钮更改色阶,例如每个按钮的最小值和最大值。我最近的比例在 0 到 50 M 之间。这对某些按钮有意义,但对于按钮 A,它没有任何意义

提前致谢

从 urllib.request 导入 urlopen 导入 json 与 urlopen("https://raw.githubusercontent.com/Babolius/project/62fef3b31fa9e34afb055e493de107d89a50a889/tr-cities-utf8.json") 作为回应: id = json.load(response) import pandas as pd df = pd.read_csv("https://raw.githubusercontent.com/Babolius/project/main/komisyon5.csv",encoding ='utf8', dtype={"Toplam": int}) df.groupby(['ID']).sum()

将 plotly.express 导入为 px

fig = px.choropleth_mapbox(df, geojson= id, locations= 'ID', color= “托普兰”, color_continuous_scale="Viridis", range_color=(0, 10000000), mapbox_style="carto-darkmatter", 缩放 = 3,中心 = {“纬度”:41.0902,“经度”:28.7129}, 不透明度=0.5, )

dropdown_buttons =[{'label': 'A', 'method' : 'restyle', 'args': [{'z': [df["A"]]}, {'visible': [True, False, False, False, False, False]}, {'title': 'A'}]}, {'label': 'B', 'method' : 'restyle', 'args': [{'z': [df["B"]]}, {'visible': [False, True, False, False , 假, 假]}, {'title': 'B'}]}, {'label': 'C', 'method' : 'restyle', 'args': [{'z': [df["C"]]}, {'visible': [False, False, True, False , 假, 假]}, {'title': 'C'}]}, {'label': 'D', 'method': 'restyle', 'args': [{'z': [df["D"]]}, {'visible': [False, False, False, True , 假, 假]}, {'title': 'D'}]}, {'label': 'E', 'method': 'restyle', 'args': [{'z': [df["E"]]}, {'visible': [False, False, False, False , 真假]}, {'title': 'E'}]}, {'label': 'Toplam', 'method' : 'restyle', 'args': [{'z': [df["Toplam"]]}, {'visible': [False, False, False, False , 假,真]},{'title': 'Toplam'}]}]

fig.update_layout({'updatemenus':[{'type': 'dropdown', 'showactive': 真的,“活动”:0,“按钮”:dropdown_buttons}]})

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

【问题讨论】:

    标签: dropdown plotly-python choropleth


    【解决方案1】:

    这比我想象的要容易

    from urllib.request import urlopen
    import json
    with urlopen("https://raw.githubusercontent.com/Babolius/project/62fef3b31fa9e34afb055e493de107d89a50a889/tr-cities-utf8.json") as response:
        id = json.load(response)
    import pandas as pd
    df = pd.read_csv("https://raw.githubusercontent.com/Babolius/project/main/komisyon5.csv",encoding ='utf8', dtype={"Toplam": int})
    df.groupby(['ID']).sum()
    
    
    import plotly.express as px
    
    
    fig = px.choropleth_mapbox(df, geojson= id, locations= 'ID', color= "Toplam",
                               color_continuous_scale="Viridis",
                               color_discrete_sequence=(min, max),
                               mapbox_style="carto-darkmatter",
                               zoom=3, center = {"lat": 41.0902, "lon": 28.7129},
                               opacity=0.5,
                              )
    
    dropdown_buttons =[{'label': 'A', 'method' : 'update', 'args': [{'z': [df["A"]]}, {'visible': [True, False, False, False, False, False]}, {'title': 'A'}]},
                       {'label': 'B', 'method' : 'update', 'args': [{'z': [df["B"]]}, {'visible': [False, True, False, False, False, False]}, {'title': 'B'}]},
                       {'label': 'C', 'method' : 'update', 'args': [{'z': [df["C"]]}, {'visible': [False, False, True, False, False, False]}, {'title': 'C'}]},
                       {'label': 'D', 'method' : 'update', 'args': [{'z': [df["D"]]}, {'visible': [False, False, False, True, False, False]}, {'title': 'D'}]},
                       {'label': 'E', 'method' : 'update', 'args': [{'z': [df["E"]]}, {'visible': [False, False, False, False, True, False]}, {'title': 'E'}]},
                       {'label': 'Toplam', 'method' : 'restyle', 'args': [{'z': [df["Toplam"]]}, {'visible': [False, False, False, False, False, True]}, {'title': 'Toplam'}]}]
    
    fig.update_layout({'updatemenus':[{'type': 'dropdown', 'showactive': True, 'active': 0, 'buttons': dropdown_buttons}]})
    
    fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      相关资源
      最近更新 更多