Skip to content

Commit 7d242e5

Browse files
committed
Update shapes.md
1 parent 03115ad commit 7d242e5

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

doc/python/shapes.md

+60-6
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ fig.show()
691691

692692
```
693693

694-
### Styling Text Labels
694+
#### Styling Text Labels
695695

696696
Use the `font` property to configure the `color`, `size`, and `family` of the label font.
697697
In this example, we change the label color of the first rectangle to "red", set the size of the text above the line to 20, and change the font family and set the font size on the second rectangle.
@@ -733,7 +733,7 @@ fig.show()
733733

734734
```
735735

736-
### Setting Label Position
736+
#### Setting Label Position
737737

738738
Set a label's position relative to the shape by setting `textposition`. The default position for lines is `middle`. The default position for other shapes is `middle center`.
739739

@@ -787,7 +787,7 @@ fig.show()
787787

788788
```
789789

790-
### Setting Label Angle
790+
#### Setting Label Angle
791791

792792
Use `textangle` to rotate a label by setting a value between -180 and 180. The default angle for a label on a line is the angle of the line. The default angle for a label on other shapes is 0. In this example, in the first shape, the label is at 45 degrees, and in the second, the label is at -45 degrees.
793793

@@ -809,16 +809,16 @@ fig.add_shape(
809809
type="rect",
810810
x0=3,
811811
y0=0,
812-
x1=4.5,
813-
y1=1.5,
812+
x1=5,
813+
y1=2,
814814
label=dict(text="Text at -45", textangle=-45),
815815
)
816816

817817
fig.show()
818818

819819
```
820820

821-
### Setting Label Padding
821+
#### Setting Label Padding
822822

823823
`padding` adds padding between the label and shape. This example shows one line with padding of 30px and another with the default padding, which is 3px.
824824

@@ -849,9 +849,63 @@ fig.add_shape(
849849
label=dict(text="No label padding"),
850850
)
851851

852+
852853
fig.show()
853854

854855
```
855856

857+
#### Setting Label Anchors
858+
859+
`xanchor` sets a label's horizontal positional anchor and `yanchor` sets its vertical position anchor.
860+
Use `xanchor` to bind the `textposition` to the "left", "center" or "right" of the label text and `yanchor` to bind `textposition` to the "top", "middle" or "bottom" of the label text.
861+
862+
In this example, `yanchor`is set to "top", instead of the default of "bottom" for lines, meaning the text displays below the line.
863+
864+
865+
```python
866+
import plotly.express as px
867+
868+
df = px.data.stocks(indexed=True)
869+
fig = px.line(df)
870+
871+
fig.add_shape(
872+
type="rect",
873+
x0="2018-09-24",
874+
y0=0,
875+
x1="2018-12-18",
876+
y1=3,
877+
line_width=0,
878+
label=dict(text="Decline", textposition="top center", font=dict(size=20)),
879+
fillcolor="green",
880+
opacity=0.25,
881+
)
882+
883+
884+
fig.add_shape(
885+
type="line",
886+
x0=min(df.index),
887+
y0=1,
888+
x1=max(df.index),
889+
y1=1,
890+
line_width=3,
891+
line_dash="dot",
892+
label=dict(
893+
text="Jan 1 2018 Baseline",
894+
textposition="end",
895+
font=dict(size=20, color="blue"),
896+
yanchor="top",
897+
),
898+
)
899+
900+
fig.show()
901+
902+
```
903+
904+
```python
905+
import plotly.graph_objects as go
906+
907+
fig = go.Figure()
908+
```
909+
856910
### Reference
857911
See https://plotly.com/python/reference/layout/shapes/ for more information and chart attribute options!

0 commit comments

Comments
 (0)