5

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?

1
  • 1
    It’s a simple matter of string formatting. Have a look at f-strings (if on Py3.6+); otherwise use string formatting. Commented Nov 6, 2020 at 18:54

2 Answers 2

3

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:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Nice one with the f-strings!
0

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:

enter image description here

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.