Skip to content

Commit ec108e0

Browse files
committed
add x-shift attributes exampple
1 parent a1f77f0 commit ec108e0

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

doc/python/shapes.md

+68-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.3'
9-
jupytext_version: 1.16.1
9+
jupytext_version: 1.16.3
1010
kernelspec:
1111
display_name: Python 3 (ipykernel)
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.10.11
23+
version: 3.10.14
2424
plotly:
2525
description: How to make SVG shapes in python. Examples of lines, circle, rectangle,
2626
and path.
@@ -579,6 +579,72 @@ fig.update_layout(
579579
fig.show()
580580
```
581581

582+
#### Shifting Shapes on Categorical Axes
583+
584+
*New in 5.23*
585+
586+
When drawing shapes where `xref` or `yref` reference axes of type category or multicategory, you can shift `x0`, `x1`, `y0`, and `y1` away from the center of the category using `x0shift`, `x1shift`, `y0shift`, and `y1shift` by specifying a value between -1 and 1.
587+
588+
-1 is the center of the previous category, 0 is the center of the referenced category, and 1 is the center of the next category.
589+
590+
In the following example, the `x0` and `x1` values for both shapes reference category values on the x-axis.
591+
592+
In this example, the first shape:
593+
- Shifts `x0` half way between the center of category "Germany" and the center of the previous category by setting `x0shift=-0.5`
594+
- Shifts `x1`half way between the center of category "Germany" and the center of the next category by setting `x1shift=0.5`
595+
596+
The second shape:
597+
- Shifts `x0` back to the center of the previous category by setting `x0shift=-1`
598+
- Shifts `x1`forward to the center of the next category by setting `x1shift=1`
599+
600+
```python
601+
import plotly.graph_objects as go
602+
import plotly.express as px
603+
604+
df = px.data.gapminder().query("continent == 'Europe' and year == 1952")
605+
606+
fig = go.Figure(
607+
data=go.Bar(x=df["country"], y=df["lifeExp"], marker_color="LightSalmon"),
608+
layout=dict(
609+
shapes=[
610+
dict(
611+
type="rect",
612+
x0="Germany",
613+
y0=0,
614+
x1="Germany",
615+
y1=0.5,
616+
xref="x",
617+
yref="paper",
618+
x0shift=-0.5,
619+
x1shift=0.5,
620+
line=dict(color="LightGreen", width=4),
621+
),
622+
dict(
623+
type="rect",
624+
x0="Spain",
625+
y0=0,
626+
x1="Spain",
627+
y1=0.5,
628+
xref="x",
629+
yref="paper",
630+
x0shift=-1,
631+
x1shift=1,
632+
line=dict(color="MediumTurquoise", width=4),
633+
),
634+
]
635+
),
636+
)
637+
638+
fig.update_layout(
639+
title="GDP per Capita in Europe (1972)",
640+
xaxis_title="Country",
641+
yaxis_title="GDP per Capita",
642+
)
643+
644+
fig.show()
645+
646+
```
647+
582648
### Drawing shapes with a Mouse on Cartesian plots
583649

584650
_introduced in plotly 4.7_

0 commit comments

Comments
 (0)