plotly:如何向现有图形添加文本?

plotly: How to add text to existing figure?

是否可以在与我的绘图相同的 html 文件中添加一些文本?

例如: 这是生成图表的代码:

data = pd.read_csv('file.csv')
data.columns = ['price', 'place', 'date']
fig = px.scatter(data, x = "place", y = "price", )
fig.write_html("done.html")

此图将在 html 文件中生成一个 pyplot 图,我想在图下添加一些简单的文本(例如解释图的结论行)。

这是我想要的输出示例: ly

您可以使用 fig.update_layout(margin=dict()) 为解释腾出空间,然后 fig.add_annotation() 在图本身下方插入您想要的任何文本以获得此内容:

完整代码:

import plotly.graph_objects as go
import numpy as np

x = np.arange(-4,5)
y=x**3

yticks=list(range(y.min(), y.max(), 14))
#yticks.append(y.max())9

# build figure
fig = go.Figure(data=go.Scatter(x=x, y=y))

# make space for explanation / annotation
fig.update_layout(margin=dict(l=20, r=20, t=20, b=60),paper_bgcolor="LightSteelBlue")

# add annotation
fig.add_annotation(dict(font=dict(color='yellow',size=15),
                                        x=0,
                                        y=-0.12,
                                        showarrow=False,
                                        text="A very clear explanation",
                                        textangle=0,
                                        xanchor='left',
                                        xref="paper",
                                        yref="paper"))

fig.show()