diff --git a/doc/python/multiple-axes.md b/doc/python/multiple-axes.md index 22974ca734f..9b2095a6389 100644 --- a/doc/python/multiple-axes.md +++ b/doc/python/multiple-axes.md @@ -352,5 +352,67 @@ fig.show() ``` +### Sync Axes Ticks + + +*New in 5.13* + +With overlayed axes, each axis by default has its own number of ticks. You can sync the number of ticks on a cartesian axis with another one it overlays by setting `tickmode="sync"`. In this example, we sync the ticks on the `"Total bill amount"` axis with the `"Total number of diners"` axis that it overlays. + +```python +import plotly.graph_objects as go +from plotly.data import tips + +df = tips() + +summed_values = df.groupby(by="day", as_index=False).sum(numeric_only=True) +day_order_mapping = {"Thur": 0, "Fri": 1, "Sat": 2, "Sun": 3} +summed_values["order"] = summed_values["day"].apply(lambda day: day_order_mapping[day]) +summed_values = summed_values.sort_values(by="order") + +days_of_week = summed_values["day"].values +total_bills = summed_values["total_bill"].values +number_of_diners = summed_values["size"].values + + +fig = go.Figure( + data=go.Bar( + x=days_of_week, + y=number_of_diners, + name="Total number of diners", + marker=dict(color="paleturquoise"), + ) +) + +fig.add_trace( + go.Scatter( + x=days_of_week, + y=total_bills, + yaxis="y2", + name="Total bill amount", + marker=dict(color="crimson"), + ) +) + +fig.update_layout( + legend=dict(orientation="h"), + yaxis=dict( + title=dict(text="Total number of diners"), + side="left", + range=[0, 250], + ), + yaxis2=dict( + title=dict(text="Total bill amount"), + side="right", + range=[0, 2000], + overlaying="y", + tickmode="sync", + ), +) + +fig.show() + +``` + #### Reference All of the y-axis properties are found here: https://plotly.com/python/reference/YAxis/. For more information on creating subplots see the [Subplots in Python](/python/subplots/) section.