通过阴谋的县长增加更多关于悬停的数据

时间:2018-09-20 15:12:38

标签: python dataframe plotly geopandas choropleth

我正在尝试在create_choropleth上使用hoverinfo属性。 我希望悬停信息在数据框中包含其他列中的数据。

我的方法如下(我已经定义了“值”和“计数”,请参见下面的完整代码),其中该行将包含在我的所有无花果参数中:

text=count, x=values, hoverinfo='text+x',

我有一个数据集,其中包含一个名为“ count”的列(我也想包括一个名为“ District”的列)。 这是我完整的代码(将hoverinfo行从无花果中移除(因为它不起作用)):

import plotly.plotly as py
from plotly.figure_factory._county_choropleth import create_choropleth
import numpy as np
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/468bd2205d85c7b0bfb4ebcd4bc4bf0ba408efb4/RRC_Spill_table/county_name%20%26%20fips%20%26%20net%20loss%20%26%20count%20(ordered%20by%20district%20%26%20grouped).csv')

colorscale = ["#f7fbff","#ebf3fb","#deebf7","#d2e3f3","#c6dbef","#b3d2e9","#9ecae1",
              "#85bcdb","#6baed6","#57a0ce","#4292c6","#3082be","#2171b5","#1361a9",
              "#08519c","#0b4083","#08306b"]

endpts = [415, 830, 1245, 1660, 2075, 2490, 2905, 3320, 3735, 4150, 4565, 4980, 5395, 5810, 6225]
fips = df['fips'].tolist()
values = df['Net spill volume (BBL)'].tolist()
count=df['number_of_oil_spills'].tolist()
x=values
fig = create_choropleth(
    fips=fips, values=values,
    binning_endpoints=endpts,
    colorscale=colorscale,
    show_state_data=False,
    show_hover=True, centroid_marker={'opacity': 0},
    scope=['TX'],
    state_outline={'color': 'rgb(15, 15, 55)', 'width': 3},
    asp=2.9, title='Oil Spills from 12/1/16 - 5/14/18',
    legend_title='Net spill Volume (BBL)'
)



fig['layout']['legend'].update({'x': 0})
fig['layout']['annotations'][0].update({'x': -0.12, 'xanchor': 'left'})
py.plot(fig, filename='oil spill net loss')

其他背景信息:

详尽的hoverinfo文档:https://plot.ly/python/reference/#scatter-hoverinfo

help(ff.create_choropleth)

关于plotly.figure_factory._county_choropleth模块中的create_choropleth函数的帮助:

:param (dict) centroid_marker: dict of attributes of the centroid marker.
    The centroid markers are invisible by default and appear visible on
    selection. See https://plot.ly/python/reference/#scatter-marker for
    all valid params

奖金问题: 不透明度甚至能做什么? 为什么我的状态显示线轮廓不显示?

1 个答案:

答案 0 :(得分:1)

我认为将其添加到代码中就可以解决问题。

 df['text'] = [str(a)+" "+str(b) for a,b in zip(count,df['District'].tolist())]
 hover_trace = [t for t in fig['data'] if 'text' in t][0]
 for i, label in enumerate(hover_trace['text']):
    # Remove FIPS
    if(i<len(fips)):
        new_label = label.replace("FIPS: %s<br>" % fips[i], "")

        # Add a new value
        new_label +=  "<br>Other: %s" % df['text'][i]

        # Update trace text
        hover_trace['text'][i] = new_label

您甚至可以执行以下操作:

 hover_trace = [t for t in fig['data'] if 'text' in t][0]
 for i, label in enumerate(hover_trace['text']):
    # Remove FIPS
    if(i<len(fips)):
        new_label = label.replace("FIPS: %s<br>" % fips[i], "")

        # Add a new value
        new_label +=  "<br>Other_1: %d" % count[i]

        # Add a new value
        new_label +=  "<br>Other_2: %s" %df['District'][i]

        # Update trace text
        hover_trace['text'][i] = new_label

有关更多信息,请参见this