3
votes

With matplotlib I could do this:

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 2, figsize=(10, 5))
fig.patch.set_facecolor('white')

axs[0].bar(x=[0,1,2], height=[1,2,3])
axs[1].plot(np.random.randint(1, 10, 50))

axs[0].set_title('BARPLOT')
axs[1].set_title('WOLOLO')

enter image description here

With plotly I know only one way to set subplot titles -- at the creation: fig = make_subplots(rows=1, cols=2, subplot_titles=('title1', 'title2'))

Is it possible to set subplots title after creation?
Maybe, through getting access to original Axes class of matplotlib (I read that plotly.python based on seaborn which is based on matplotlib)?
Or through fig.layout?

Please use this code:

import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(y=[1, 2, 3]), row=1, col=1)
fig.add_trace(go.Scatter(y=np.random.randint(1, 10, 50)), row=1, col=2)

enter image description here

1

1 Answers

3
votes

The following code does the trick you want.

import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2, subplot_titles=("Plot 1", "Plot 2"))
fig.add_trace(go.Bar(y=[1, 2, 3]), row=1, col=1)
fig.add_trace(go.Scatter(y=np.random.randint(1, 10, 50)), row=1, col=2)

fig.layout.annotations[1].update(text="Stackoverflow")

fig.show()

I got this idea from https://community.plotly.com/t/subplot-title-alignment/33210/2 when it's described how to access each plot's annotation.