Plotly Graph Object Subplots - vrect not working

The vrect example below does not work with subplots. e.g. If the fig (Figure object) is constructed as go.Fugure() the code works. But fig object returned by make_subplots() doesn’t.

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

# fig = make_subplots(rows=2, cols=1,
#                     column_widths=[1],
#                     row_heights=[0.15, 0.85],
#                     specs=[[{'type': 'xy'}], [{'type': 'table'}]],
#                     vertical_spacing=0.08
#                     )
fig = go.Figure()

# Add scatter trace for line
fig.add_trace(go.Scatter(
    x=["2015-02-01", "2015-02-02", "2015-02-03", "2015-02-04", "2015-02-05",
       "2015-02-06", "2015-02-07", "2015-02-08", "2015-02-09", "2015-02-10",
       "2015-02-11", "2015-02-12", "2015-02-13", "2015-02-14", "2015-02-15",
       "2015-02-16", "2015-02-17", "2015-02-18", "2015-02-19", "2015-02-20",
       "2015-02-21", "2015-02-22", "2015-02-23", "2015-02-24", "2015-02-25",
       "2015-02-26", "2015-02-27", "2015-02-28"],
    y=[-14, -17, -8, -4, -7, -10, -12, -14, -12, -7, -11, -7, -18, -14, -14,
       -16, -13, -7, -8, -14, -8, -3, -9, -9, -4, -13, -9, -6],
    mode="lines",
    name="temperature"
))
#row=1, col=1)

# Add shape regions
fig.add_vrect(
    x0="2015-02-04", x1="2015-02-13",
    fillcolor="LightSalmon", opacity=0.5,
    layer="below", line_width=0,
),
fig.show()

ValueError:
Cannot add shape to subplot at position (2, 1) because subplot
is of type domain.

This limits highlight features only to standalone plots vs mixed subplots (plotly.subplots)

Anyone else facing this issue?

I ran your code in my environment. The correct rectangular area is displayed on the graph. From the error message, it may be affected by the cache or other factors that executed the code for the subplot. You might try running a kernel restart.

@pa3m
fig.add_vrect() works like a charm when you are setting all needed information/attributes. Just
inspect

help(go.Figure.add_vrect)

that displays the following information:

add_vrect(self, x0, x1, row='all', col='all', exclude_empty_subplots=True, annotation=None, **kwargs) -> 'Figure'
    Add a rectangle to a plot or subplot that extends infinitely in the
    y-dimension.
    
    Parameters
    ----------
    x0: float or int
        A number representing the x coordinate of one side of the rectangle.
    x1: float or int
        A number representing the x coordinate of the other side of the rectangle.
    exclude_empty_subplots: Boolean
        If True (default) do not place the shape on subplots that have no data
        plotted on them.
    row: None, int or 'all'
        Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
        the specified column(s). If both row and col are None, addresses the
        first subplot if subplots exist, or the only plot. By default is "all".
    col: None, int or 'all'
        Subplot column for shape indexed starting at 1. If 'all', addresses all rows in
        the specified column(s). If both row and col are None, addresses the
        first subplot if subplots exist, or the only plot. By default is "all".
    annotation: dict or plotly.graph_objects.layout.Annotation. If dict(),
        it is interpreted as describing an annotation. The annotation is
        placed relative to the shape based on annotation_position (see
        below) unless its x or y value has been specified for the annotation
        passed here. xref and yref are always the same as for the added
        shape and cannot be overridden.
    annotation_position: a string containing optionally ["inside", "outside"], ["top", "bottom"]
        and ["left", "right"] specifying where the text should be anchored
        to on the rectangle. Example positions are "outside top left", "inside
        bottom", "right", "inside left", "inside" ("outside" is not supported). If
        an annotation is added but annotation_position is not specified this
        defaults to "inside top right".
    annotation_*: any parameters to go.layout.Annotation can be passed as
        keywords by prefixing them with "annotation_". For example, to specify the
        annotation text "example" you can pass annotation_text="example" as a
        keyword argument.
    **kwargs:
        Any named function parameters that can be passed to 'add_shape',
        except for x0, x1, y0, y1 or type.

Hence in your subplot cell (1,1) you must add:

fig.add_vrect(
    x0="2015-02-04", x1="2015-02-13",
    fillcolor="LightSalmon", opacity=0.5, row=1, col=1, 
    layer="below", line_width=0,
)  #IN your code above you have a comma after the last paranthesis.  REMOVE IT

1 Like

Thank you empet, Adding row and col params to add_vrect() fixed the issue.
Much Appreciated.