Plotly:如何在图中包含多个文本变量?

Plotly: How to include multiple text variables in plot?

如何在 iplot() 中显示多个文本变量? 这是一个例子:

df2007.iplot(kind='bubble', x='gdpPercap', y='lifeExp', size='pop', text='country',
             xTitle='GDP per Capita', yTitle='Life Expectancy',
             filename='cufflinks/simple-bubble-chart')

取自此处:https://plot.ly/ipython-notebooks/cufflinks/#scatter-matrix

我想向文本添加另一个变量,传递列名列表没有帮助。 我试过了

text = 'country' + <br/> + 'country'

为了实现这一目标,您必须摆脱 px.Scatter 的束缚。但是,如果您愿意,可以将 go.Scatteriplot() 结合使用。下面的代码片段将向您展示如何通过 formatting hovertemplate 正确并使用 go.Scatter.[=18 的 text 属性 在您的悬停文本中分配一个带有自己数据的额外变量=]

剧情:

完整代码:

import plotly.express as px
from plotly.offline import iplot
import plotly.graph_objects as go

# data
gapminder = px.data.gapminder()
df=gapminder.query("year==2007").copy(deep=True)
df['extraVar']=[i**4 for i in range(len(df))]

# plotly
fig=go.Figure()
fig.add_trace(go.Scatter(x=df['gdpPercap'], y=df['lifeExp'],
                         mode='markers',
                         marker=dict(size=df['pop'],sizemode='area',
                                sizeref=2.*max(df['pop'])/(40.**2),sizemin=2
                                    ),
                         hovertemplate =
                                        '<i>lifeExp</i>: $%{y:.2f}'+
                                        '<br><i>gdpPercap</i>: %{x}<br>'+
                                        '<i>%{text}</i>',
                         text = ['Your extra variable {}'.format(i) for i in df['extraVar']],
                        )
             )

# iplot
iplot(fig)