Plotly:如何修改直方图的悬停模板?
Plotly: How to modify hovertemplate of a histogram?
我想修改直方图的悬停模板。到目前为止,找不到方法:
舍入给定的值
删除直方图的名称(此处为:'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>'
部分既酷又神秘,只需从悬停标签中删除 2012
和 2013
。
我希望这接近您想要实现的目标。不过,我必须仔细看看舍入部分。不过总的来说我觉得剧情还是挺好看的
一些细节和意见:
据我所知,与 plotly.figure_factory.create_distplot 上 github 的声明相反,ff.create_distplot
实际上并没有走向弃用。只是不再更新了。此外,似乎 ff.create_distplot
仍然是创建添加了正态分布或 kde 估计的直方图的最佳选择。所以在可预见的未来它不会去任何地方。
完整代码:
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()
我想修改直方图的悬停模板。到目前为止,找不到方法: 舍入给定的值 删除直方图的名称(此处为:'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>'
部分既酷又神秘,只需从悬停标签中删除 2012
和 2013
。
我希望这接近您想要实现的目标。不过,我必须仔细看看舍入部分。不过总的来说我觉得剧情还是挺好看的
一些细节和意见:
据我所知,与 plotly.figure_factory.create_distplot 上 github 的声明相反,ff.create_distplot
实际上并没有走向弃用。只是不再更新了。此外,似乎 ff.create_distplot
仍然是创建添加了正态分布或 kde 估计的直方图的最佳选择。所以在可预见的未来它不会去任何地方。
完整代码:
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()