【发布时间】: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