Skip to content

Commit ec972cc

Browse files
facet annotations
1 parent 8808e8b commit ec972cc

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

doc/python/facet-plots.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,56 @@ fig = px.histogram(df, x="total_bill", y="tip", color="sex", facet_row="time", f
8383
fig.show()
8484
```
8585

86+
### Adding Lines and Rectangles to Facet Plots
87+
88+
It is possible to add [labelled horizontal and vertical lines and rectangles](/python/horizontal-vertical-shapes/) to facet plots using `.add_hline()`, `.add_vline()`, `.add_hrect()` or `.add_vrect()`. The default `row` and `col` values are `"all"` but this can be overridden, as with the rectangle below, which only appears in the first column.
89+
90+
```python
91+
import plotly.express as px
92+
93+
df = px.data.stocks(indexed=True)
94+
fig = px.line(df, facet_col="company", facet_col_wrap=2)
95+
fig.add_hline(y=1, line_dash="dot",
96+
annotation_text="Jan 1, 2018 baseline",
97+
annotation_position="bottom right")
98+
99+
fig.add_vrect(x0="2018-09-24", x1="2018-12-18", col=1,
100+
annotation_text="decline", annotation_position="top left",
101+
fillcolor="green", opacity=0.25, line_width=0)
102+
fig.show()
103+
104+
```
105+
106+
### Adding the Same Trace to All Facets
107+
108+
The `.add_trace()` method can be used to add a copy of the same trace to each facet, for example an overall linear regression line as below. The `legendgroup`/`showlegend` pattern below is recommended to avoid having a separate legend item for each copy of the trace.
109+
110+
```python
111+
import plotly.express as px
112+
df = px.data.tips()
113+
fig = px.scatter(df, x="total_bill", y="tip", color='sex',
114+
facet_col="day", facet_row="time")
115+
116+
import statsmodels.api as sm
117+
import plotly.graph_objects as go
118+
df = df.sort_values(by="total_bill")
119+
model = sm.OLS(df["tip"], sm.add_constant(df["total_bill"])).fit()
120+
121+
#create the trace to be added to all facets
122+
trace = go.Scatter(x=df_sorted["total_bill"], y=model.predict(),
123+
line_color="black", name="overall OLS")
124+
125+
# give it a legend group and hide it from the legend
126+
trace.update(legendgroup="trendline", showlegend=False)
127+
128+
# add it to all rows/cols, but no to empty subplots
129+
fig.add_trace(trace, row="all", col="all", exclude_empty_subplots=True)
130+
131+
# set only the last trace added to appear in the legend
132+
fig.data[-1].update(showlegend=True)
133+
fig.show()
134+
```
135+
86136
### Facets With Independent Axes
87137

88138
By default, facet axes are linked together: zooming inside one of the facets will also zoom in the other facets. You can disable this behaviour when you use `facet_row` only, by disabling `matches` on the Y axes, or when using `facet_col` only, by disabling `matches` on the X axes. It is not recommended to use this approach when using `facet_row` and `facet_col` together, as in this case it becomes very hard to understand the labelling of axes and grid lines.
@@ -103,7 +153,7 @@ fig.update_xaxes(matches=None)
103153
fig.show()
104154
```
105155

106-
### Customize Subplot Figure Titles
156+
### Customizing Subplot Figure Titles
107157

108158
Since subplot figure titles are [annotations](https://plotly.com/python/text-and-annotations/#simple-annotation), you can use the `for_each_annotation` function to customize them, for example to remove the equal-sign (`=`).
109159

0 commit comments

Comments
 (0)