python - Plotly:如何修改直方图的悬停模板?

标签 python plotly plotly-python

我想修改直方图的悬停模板。到目前为止,找不到方法: 舍入给定的值 删除直方图的名称(此处为:'2012'、'2013') 添加名称以指示值代表什么

所需的悬停文本:

Bin-range: -1.24, 3.31

更好的选择(带连字符的范围):

Bin-range: -1.24 - 3.31

有什么想法吗?

import plotly.figure_factory as ff
import numpy as np
import pandas as pd

df = pd.DataFrame({'2012': np.random.randn(200),
                   '2013': np.random.randn(200)+1})

fig = ff.create_distplot([df[c] for c in df.columns], 
                         df.columns, bin_size=.25, show_rug=False)
fig.show()

最佳答案

答案:

将以下代码片段添加到您的设置中将生成如下图:

fig.update_traces(hovertemplate ='<i>Bin-range</i>:' + '%{x}' + '<extra></extra>',
                  selector=dict(type="histogram"))

部分selector=dict(type="histogram")是否只更新直方图轨迹并单独留下线条。 '<extra></extra>'部分既酷又神秘,只需删除 20122013来自悬停标签。

enter image description here

我希望这接近您想要实现的目标。不过,我必须仔细看看舍入部分。但总的来说,我认为 plotly 看起来还不错

一些细节和评论:

据我所知,与 github 上关于 plotly.figure_factory.create_distplot 的声明相反, ff.create_distplot实际上并没有走向弃用。只是不再更新了。此外,似乎 ff.create_distplot仍然是创建带有正态分布或添加了 kde 估计的直方图的最佳选择。因此,在可预见的 future ,它不会有任何进展。

完整代码:

import plotly.figure_factory as ff
import numpy as np
import pandas as pd

np.random.seed(123)
df = pd.DataFrame({'2012': np.random.randn(200),
                   '2013': np.random.randn(200)+1})

fig = ff.create_distplot([df[c] for c in df.columns], 
                         df.columns, bin_size=.25, show_rug=False)

fig.update_traces(hovertemplate ='<i>Bin-range</i>:' + '%{x}' + '<extra></extra>',
                  selector=dict(type="histogram"))

#fig.update_layout(hoverinfo = 'x+y+text')

fig.show()

关于python - Plotly:如何修改直方图的悬停模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62448872/

相关文章:

python - Plotly:如何只显示点击的行?

python - 删除 Plotly Express 中自定义悬浮卡右侧的颜色

python - 根据列值删除数据框中的行

python - 如何使用 Selenium Xpath 在一页中查找具有相同路径的多个元素

python - plotly :带有实时图表的破折号按钮不起作用

python - 在 Jupyter Notebook 中保存 Plotly 离线图像?

R绘图显示光标坐标

python - 使用绘图对象创建显示目录结构的树形图

python - FreeBSD 上 Python 中的 psutil 错误

python - 如何让virtualenv Django服务器工作?//根据命令提示符 'missing attribute'