and then proceed to populate them by adding traces with appropraite row and col keywords.
We would like to extract one of the subplots to use in a new figure. Construction of the plot takes a bit of time and it should be faster to extract a specific row and column from the figure data. We had hoped that the subplot would be relatively self-contained and easy to extract, but it is not obvious that this is the case. Is there a simple way to retrieve a specific subplot and bind it to a Figure that we are returning?
Thank you @AIMPED, this was helpful indeed. I ended up using an internal method on the figure, _get_subplot_rows_columns to access the geometry and then select_traces with a specific row and column:
def plot_subfigure(subplot, fig_subplots):
"""
Retrieve a specific figure subplot and return it as a new figure
:param subplot: Index of subplot 1:N
:param fig_subplots: Figure with subplots
:return: New figure
"""
# Get the number of rows and columns
row_geom, col_geom = fig_subplots._get_subplot_rows_columns()
# Column major order, so plots per column will be our divisor
plots_per_col = len(list(col_geom))
# Compute row/col for referenced subplot
subplot = subplot - 1 # 1:N --> 0:N-1
r = int(subplot / plots_per_col) + 1
c = subplot % plots_per_col + 1
# select traces associated with the subplot and add them to new Fig
traces = []
generator = self.current_subplots[0].select_traces(row=r, col=c)
for t in generator:
traces.append(t)
fig = go.Figure(data=traces)
# Update axes - Retrieve axis information for x and y
# then copy axis information to the new figure's axes
for upd, get in zip([fig.update_xaxes, fig.update_yaxes],
[fig_subplots.select_xaxes, fig_subplots.select_yaxes]):
ax = [a for a in get(row=r, col=c)][0]
upd(ax) # copy properties
# See if the subplot had a restricted viewport
if 'domain' in ax:
upd({'domain': (0, 1)}) # Expand to fill plot
return fig