是否可以在与我的绘图图相同的 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()
关于python - plotly:如何向现有图形添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62716521/