Skip to content

Add tickmode="sync" example #4028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions doc/python/multiple-axes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.