gpt4 book ai didi

python - 通过绘制县等值区域图添加更多关于悬停的数据

转载 作者:行者123 更新时间:2023-12-01 09:01:24 25 4
gpt4 key购买 nike

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

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

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')

其他背景信息:

绘图悬停信息文档: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

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

最佳答案

我认为在绘图之前将其添加到您的代码中可以解决您的问题。

 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

关于python - 通过绘制县等值区域图添加更多关于悬停的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52428311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com