Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Plotly:如何使用Plotly.express piechart向悬停数据添加元素?_Python_Plotly - Fatal编程技术网

Python Plotly:如何使用Plotly.express piechart向悬停数据添加元素?

Python Plotly:如何使用Plotly.express piechart向悬停数据添加元素?,python,plotly,Python,Plotly,我正在使用plotly.express中的示例,并尝试向hover_数据属性添加一个额外的元素iso_num(iso_num是gapminder数据框中的一个int64列) 将鼠标悬停在饼图的切片上,可得出以下结果: 其中,isonum值是%{customdata[1]}而不是列中的数值 我错过了什么 谢谢 这似乎是一个从后面说的话 哦,馅饼悬停真是一团糟 从那以后似乎一直如此。但是对于px.pie(),可能不是这样的? 我已经尝试了许多方法,但我只能使该方法适用于go.Pie,而不适用于px

我正在使用plotly.express中的示例,并尝试向hover_数据属性添加一个额外的元素
iso_num
iso_num
是gapminder数据框中的一个int64列)

将鼠标悬停在饼图的切片上,可得出以下结果:

其中,
isonum
值是
%{customdata[1]}
而不是列中的数值

我错过了什么

谢谢

这似乎是一个从后面说的话

哦,馅饼悬停真是一团糟

从那以后似乎一直如此。但是对于
px.pie()
,可能不是这样的? 我已经尝试了许多方法,但我只能使该方法适用于
go.Pie
,而不适用于
px.Pie
。下面演示如何将值分配给
customdata
将使任何未分配给
go.Pie()的变量可用于自定义鼠标悬停板:

绘图:

import plotly.graph_objects as go
import plotly.express as px

df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'")

fig = go.Figure(go.Pie(
    name = "",
    values = df['pop'],
    labels = df['country'],
    customdata=df['iso_num'],
    hovertemplate = "Country:%{label}: <br>Population: %{value} </br> iso num:%{customdata}"

))
fig.show()

代码:

import plotly.graph_objects as go
import plotly.express as px

df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'")

fig = go.Figure(go.Pie(
    name = "",
    values = df['pop'],
    labels = df['country'],
    customdata=df['iso_num'],
    hovertemplate = "Country:%{label}: <br>Population: %{value} </br> iso num:%{customdata}"

))
fig.show()
导入plotly.graph\u对象
将plotly.express导入为px
df=px.data.gapminder()
图=开始图(开始图)(
name=“”,
值=df['pop'],
标签=df[“国家”],
customdata=df['iso_num'],
hovertemplate=“国家:%%{label}:
人口:%%{value}
isonum:%%{customdata}” )) 图2(图3)
我也找到了一种使用Plotly Express饼图的方法。您可以使用
更新跟踪
来定义
悬停模板
。对于
hover\u data
/
custom\u data
的多个值进行拆分似乎存在问题,并且所有值仅存在于0索引中,即两个值都位于
customdata[0]

import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'")
fig = px.pie(df, values='pop', names='country',
             title='Population of American continent',
             custom_data=['lifeExp','iso_num'], labels={'lifeExp':'life expectancy','iso_num':'iso num'
                                                      })
fig.update_traces(textposition='inside', textinfo='percent+label',\
                 hovertemplate = "Country:%{label}: <br>Population: %{value} </br>(life expentancy, iso num) : %{customdata}"
)

fig.show()
将plotly.express导入为px
df=px.data.gapminder()
图=px.pie(df,value='pop',name='country',
title='美洲大陆的人口',
自定义_数据=['lifeExp','iso_num'],标签={'lifeExp':'lifeempeating','iso_num':'iso num'
})
图更新跟踪(textposition='inside',textinfo='percent+label'\
hovertemplate=“国家:%{label}:
人口:%{value}
(生活体验,iso数量):%{customdata}” ) 图2(图3)
悬停时:


我的建议对你有何作用?我正在尝试!谢谢你接受我的回答。关于向悬停数据添加元素,您还想知道什么吗?非常感谢您的回复。事实上,当我将您的解决方案复制到jupyter笔记本中时,弹出窗口会显示预期的字段。我的原始问题不够精确:我需要在Dash应用程序中使用此piechart。现在我需要弄清楚如何成功地使用
go.Pie
而不是Dash中的plotly.express。谢谢@我看到有人埋伏了。我认为这应该很好。这似乎是一个我们可以修复的bug:)