Python - 情节悬停图标不起作用

时间:2017-02-20 18:05:53

标签: python plotly

据我所知,我已经完全复制了文档。我基本上使用了文档代码并根据我的目的进行了调整。但是当我运行这段代码时,我的情节中没有出现带文字的悬停功能。

#Initialize df
aviation_data = pd.DataFrame(columns=["Latitude","Longitude","Fatalities"])
aviation_data["Latitude"] = [40.53666,60.94444]
aviation_data["Longitude"] = [-81.955833,-159.620834]
aviation_data["Fatalities"] = [True,False]

#Initialize colorscale
scl = [[0,"rgb(216,15,15)"],[1,"rgb(5,10,172)"]]

#Initialize text data
text_df = "Fatal: " + aviation_data["Fatalities"].apply(lambda x: str(np.bool(x))) + '<br>' + \
    "Latitude: " + aviation_data["Latitude"].apply(lambda x: str(x)) + '<br>' + \
    "Longitude" + aviation_data["Longitude"].apply(lambda x: str(x))

#Initialize data
data = [ dict(
    type = 'scattergeo',
    locationmode = 'USA-states',
    lon = aviation_data["Longitude"],
    lat = aviation_data["Latitude"],
    text = text_df,
    mode = 'markers',
    marker = dict( 
        size = 5, 
        opacity = 0.5,
        reversescale=True,
        autocolorscale=False,
        symbol = 'circle',
        line = dict(
            width=1,
            color='rgba(102, 102, 102)'
        ),
        colorscale = scl,
        cmin = 0,
        color = aviation_data["Fatalities"].astype(int),
        cmax = 1
    ))]

#Initialize layout
layout = dict(
    title ='Aviation Incidents for the Years 2014-2016<br>\
    (red indicates fatal incident, blue indicates non-fatal)',
    geo = dict(
        scope='usa',
        projection=dict(type='albers usa'),
        showland = True,
        landcolor = "rgb(206, 206, 206)",
        countrywidth = 0.5,
        subunitwidth = 0.5        
    ),
)

#Plot
fig = dict(data=data,layout=layout)
iplot(fig,validate=False)

任何人都知道为什么我的悬停文字没有出现?

1 个答案:

答案 0 :(得分:0)

在代码的最后一行中,您需要调用此代码:

plotly.offline.plot(fig, validate=False)

代替:

iplot(fig, validate=False)

也不要忘记密谋导入:

import plotly

希望这会有所帮助