简体   繁体   中英

Plotly: How to assign a variable to subplot titles?

I want to display extra data on several subplots and decided to do so in the subplot titles.

I have figured out how to add a title to the subplots, but am not able to include a variable in each one. The code so far is:

fig = make_subplots(rows=3, cols=1, subplot_titles=("Share Price is: ", "RSI is: ", "Portfolio Return is: "))

I want to add the variables at the end of each subplot title.

How can this be done?

This little bit of code will get you there. Essentially, just use string formatting (or f-strings if on Python 3.6+).

Note the variable declarations at the beginning, then the f-string substitution in the titles tuple. As you'll notice, since strings are being used, the subplot titles can contain monetary values, percentages, decimal values ... whatever suites the purpose. These can even be populated directly from values in your dataset.

Sample code:

from plotly.subplots import make_subplots

shr = '£25.10'
rsi = '40%'
rtn = '12'

# Use f-strings to format the subplot titles.
titles = (f'Share Price is: {shr}', 
          f'RSI is: {rsi}', 
          f'Portfolio Return is: {rtn}')

fig = make_subplots(rows=3, 
                    cols=1, 
                    subplot_titles=titles)

fig.add_trace({'y': [1, 2, 3, 4, 5], 'name': 'Share Price'}, row=1, col=1)
fig.add_trace({'y': [5, 4, 2, 3, 1], 'name': 'RSI'}, row=2, col=1)
fig.add_trace({'y': [1, 4, 2, 3, 5], 'name': 'Return'}, row=3, col=1)

fig.show()

Output:

在此处输入图片说明

In your particular case I would just organize the different new elemenst in a dict:

infos = {'price':29,
         'RSI': 1.1,
         'return':1.1}

And then subset that dict in make_subplots() like this:

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'])))

Why the dict? I often find myself in situations where I'd like to do things with plot elements after the figure is defined. And this way your new elements will be readily available for other purposes as well.

Plot:

在此处输入图片说明

Complete code:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM