plotly:如何向现有图形添加文本? [英] plotly: How to add text to existing figure?

查看:159
本文介绍了plotly:如何向现有图形添加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

data = pd.read_csv('file.csv')data.columns = ['价格','地点','日期']fig = px.scatter(data, x = "place", y = "price", )fig.write_html("done.html")

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

这是我想要的输出示例:

完整代码:

import plotly.graph_objects as go将 numpy 导入为 npx = np.arange(-4,5)y=x**3yticks=list(range(y.min(), y.max(), 14))#yticks.append(y.max())9# 构建图形fig = go.Figure(data=go.Scatter(x=x, y=y))# 为解释/注释留出空间fig.update_layout(margin=dict(l=20, r=20, t=20, b=60),paper_bgcolor=LightSteelBlue")# 添加注释fig.add_annotation(dict(font=dict(color='yellow',size=15),x=0,y=-0.12,显示箭头=假,text="一个非常清楚的解释",文字角度=0,xanchor='左',外部参照 =纸",yref="纸"))图.show()

Is it possible to add some text on the same html file as my plotly graph?

For example : This is the code that generates a graph :

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

This graph will generate a pyplot graph in an html file and I want to add some simple text (such as a conclusion line explaning the graph) under the graph.

This is an example of the output I would like: ly

解决方案

You can use fig.update_layout(margin=dict()) to make room for an explanation, and then fig.add_annotation() to insert any text you'd like below the figure utself to get this:

Complete code:

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()

这篇关于plotly:如何向现有图形添加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆