Plotly 文本模板似乎忽略了实际的字符串模板

问题描述 投票:0回答:2

文档的这一部分中,我期望以下代码生成一个条形图,其中每个条形都包含根据

df['label']
相应的文本。相反,每个条都包含在
df['Foo']
中找到的字符串。如何让
texttemplate
正常工作,即让每个栏都包含
df['label']
中的文本?

>>> import plotly
>>> import pandas as pd
>>> import plotly.graph_objects as go

>>> plotly.__version__ 
5.17.0

>>> df = pd.DataFrame(data={"Foo":['a','b','c'],'Bar':[2,5,10], 'label':['alpha','beta', 'charlie']})
df
        Foo     Bar     label
    0   a       2       alpha
    1   b       5       beta
    2   c       10      charlie

>>> bar = go.Bar(x=df['Foo'], y=df['Bar'], texttemplate ="%{label}")
>>> go.Figure(data=bar)

python plotly plotly-python
2个回答
0
投票

您可以尝试通过 text 更改 texttemplate,如下所示:

bar = go.Bar(x=df['Foo'], y=df['Bar'], text =df['label'])

0
投票

第一个答案非常接近。您必须将标签名称放入

text=
属性中并在
texttemplate=

中使用它
bar = go.Bar(x=df['Foo'], y=df['Bar'],
             text = df['label'],
             texttemplate = "labeltext: %{label}<br>my text: %{text}",)

© www.soinside.com 2019 - 2024. All rights reserved.