python Plotly texttemplate似乎忽略实际字符串模板

46scxncf  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(111)

documentation的这一部分,我期望以下代码生成一个条形图,其中每个条形图都包含与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)

igsr9ssn

igsr9ssn1#

您可以尝试将texttemplate改为text,如下所示:

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

相关问题