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

标签 python plotly plotly-python

是否可以在与我的绘图图相同的 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() 在图下方插入您想要的任何文本以获取此信息:
enter image description here
完整代码:

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/

相关文章:

python - 类里面奇怪的列表行为

r - Plotly - 使用子图塑造位置

python - Plotly-Dash:下拉选项可以工作,但不会绘制数据

python - Plotly:如何制作 3D 堆叠直方图?

python - 检查 Plone PythonScript 中的类型

javascript - 将值传递给搜索栏

python - 左右翻转 Plotly 水平直方图

javascript - plotly.js 条形图在点击条形图时需要 onclick 事件

python - 为什么单击下拉菜单选项会重置 x 范围?

python - Django 小部件模板覆盖不会在项目模板目录中搜索。怎么修?