【问题标题】:adding more data on hover with a plotly county choropleth添加更多关于悬停的数据
【发布时间】:2018-09-20 15:12:37
【问题描述】:

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

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

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

我有一个数据集,其中包含名为“count”的列(我还想包含名为“District”的列)。 这是我的完整代码(从 fig 中删除了 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')

其他背景信息:

plotly hoverinfo 文档:https://plot.ly/python/reference/#scatter-hoverinfo

帮助(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

奖励问题: 不透明度甚至有什么作用? 为什么我的状态线轮廓不显示?

【问题讨论】:

    标签: python dataframe plotly geopandas choropleth


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

    【讨论】:

    • 谢谢,这是进步。由于某种原因,它为每个县分配了错误的地区和计数,并且只为少数几个县添加了这些新值,也没有删除 fips。也许它的索引错误。
    • 我会在星期一进一步研究并尝试更新答案
    • 那么当我定义一个新值时,[i] 最后的作用是什么?
    • 更新一个特定的值而不是整个列表
    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多