在您的情况下,我会将不同的新元素组织到一个字典中:
infos = {'price':29,
'RSI': 1.1,
'return':1.1}
然后在make_subplots()
中对该字典进行子集操作,如下所示:
fig = make_subplots(rows=3, cols=1, start_cell="top-left",
subplot_titles=("Share Price is: "+ str(infos['price']),
"RSI is: " + str(infos['RSI']),
"Portfolio Return is: " + str(infos['return'])))
为什么要用字典?我经常发现自己在定义图形后想对绘图元素进行操作。这样,您的新元素也将随时可用于其他目的。
绘图:
![enter image description here](https://istack.dev59.com/bQnSY.webp)
完整代码:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
infos = {'price':29,
'RSI': 1.1,
'return':1.1}
fig = make_subplots(rows=3, cols=1, start_cell="top-left",
subplot_titles=("Share Price is: "+ str(infos['price']),
"RSI is: " + str(infos['RSI']),
"Portfolio Return is: " + str(infos['return'])))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
row=1, col=1)
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
row=2, col=1)
fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]),
row=3, col=1)
fig.show()
f2 = fig.full_figure_for_development(warn=False)