如何访问情节/氯空白地图的悬停国家名称。有类似吸气剂的方法吗?

这可能是一个完全愚蠢的问题,但是我无法在python中访问plotly对象的某些字段。到目前为止,我刚与Java进行过很多合作,而我的新大学希望我编写一些python的东西,而这些东西可能卡在了我的Java思维方式中。

问题:

如何访问绘图/氯体积图的内部字段?

代码示例:

首先,我导入情节表达并创建一个情节世界地图,以后我要用它来选择一个国家

import plotly.express as px
gapminder = px.data.gapminder().query("year == 2007")
mapfig = px.scatter_geo(gapminder,locations="iso_alpha",hover_name="country",size="pop")

后来我通过html形象化了数字

html.Div(
    dcc.Graph(
        id='country-selector',figure=mapfig,)
)

当我现在运行python脚本时,我会看到一个世界地图,并且可以将鼠标悬停在任何国家/地区,看到带有人口和县名的弹出窗口。到目前为止,一切都很好。现在要解决的问题:当我尝试更新其他图形时,我稍后无法在代码中访问国家/地区或hover_name。

我想象的样子:

html.Div(
    dcc.Graph(
        id='country-selector',hover_name=  --> here i need some kind of java style getter on the hover_name of mapfig
    )
)

@app.callback(
    dash.dependencies.Output('graph-indicator-1','figure'),[dash.dependencies.Input('country-selector','hover_name')])

def update_other_graph(hover_name,selected_value_for_indicator_1):

--> here i want to work with hover_name (the name of the hovered country of my world map)

也许有人比我更精通python,并且知道答案。谢谢您的时间!

raincraft 回答:如何访问情节/氯空白地图的悬停国家名称。有类似吸气剂的方法吗?

您可以像这样从hoverData访问国家名称:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px

app = dash.Dash()

gapminder = px.data.gapminder().query("year == 2007")
mapfig = px.scatter_geo(gapminder,locations="iso_alpha",hover_name="country",size="pop")
app.layout = html.Div([
    dcc.Graph(
        id='country-selector',figure=mapfig,),],className="container")

@app.callback(
    dash.dependencies.Output('country-selector','figure'),[dash.dependencies.Input('country-selector','hoverData')],[dash.dependencies.State('country-selector','figure')]
)
def drawStockPrice(hover_data,figure):
    data = figure['data']
    layout = figure['layout']

    if hover_data is not None:
        print('Country:',hover_data['points'][0]['hovertext'])

    return {'data': data,'layout': layout}

if __name__ == '__main__':
    app.run_server(debug=True)
本文链接:https://www.f2er.com/3115268.html

大家都在问