Skip to content

Commit 133ac09

Browse files
Merge pull request #4028 from plotly/example-next-release
Add tickmode="sync" example
2 parents 7062060 + 36cc0ea commit 133ac09

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Diff for: doc/python/multiple-axes.md

+62
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,67 @@ fig.show()
352352

353353
```
354354

355+
### Sync Axes Ticks
356+
357+
358+
*New in 5.13*
359+
360+
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.
361+
362+
```python
363+
import plotly.graph_objects as go
364+
from plotly.data import tips
365+
366+
df = tips()
367+
368+
summed_values = df.groupby(by="day", as_index=False).sum(numeric_only=True)
369+
day_order_mapping = {"Thur": 0, "Fri": 1, "Sat": 2, "Sun": 3}
370+
summed_values["order"] = summed_values["day"].apply(lambda day: day_order_mapping[day])
371+
summed_values = summed_values.sort_values(by="order")
372+
373+
days_of_week = summed_values["day"].values
374+
total_bills = summed_values["total_bill"].values
375+
number_of_diners = summed_values["size"].values
376+
377+
378+
fig = go.Figure(
379+
data=go.Bar(
380+
x=days_of_week,
381+
y=number_of_diners,
382+
name="Total number of diners",
383+
marker=dict(color="paleturquoise"),
384+
)
385+
)
386+
387+
fig.add_trace(
388+
go.Scatter(
389+
x=days_of_week,
390+
y=total_bills,
391+
yaxis="y2",
392+
name="Total bill amount",
393+
marker=dict(color="crimson"),
394+
)
395+
)
396+
397+
fig.update_layout(
398+
legend=dict(orientation="h"),
399+
yaxis=dict(
400+
title=dict(text="Total number of diners"),
401+
side="left",
402+
range=[0, 250],
403+
),
404+
yaxis2=dict(
405+
title=dict(text="Total bill amount"),
406+
side="right",
407+
range=[0, 2000],
408+
overlaying="y",
409+
tickmode="sync",
410+
),
411+
)
412+
413+
fig.show()
414+
415+
```
416+
355417
#### Reference
356418
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.

0 commit comments

Comments
 (0)