Skip to content

Commit 3ccc3f1

Browse files
committed
Update shapes.md
1 parent 2faa203 commit 3ccc3f1

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

doc/python/shapes.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,5 +665,193 @@ fig.show(config={'modeBarButtonsToAdd':['drawline',
665665
]})
666666
```
667667

668+
### Adding Text Labels to Shapes
669+
670+
*New in 5.14*
671+
672+
Add a text `label` to a shape by adding a `label` property to a shape with `text`. In this example, we add a `rect` and `line` shape and add a text label to both.
673+
674+
```python
675+
import plotly.graph_objects as go
676+
677+
fig = go.Figure()
678+
679+
fig.add_shape(type="rect", x0=1, y0=1, x1=2, y1=3, label=dict(text="Text in rectangle"))
680+
fig.add_shape(
681+
type="line",
682+
x0=3,
683+
y0=0.5,
684+
x1=5,
685+
y1=0.8,
686+
line_width=3,
687+
label=dict(text="Text above line"),
688+
)
689+
690+
fig.show()
691+
692+
```
693+
694+
### Styling Text Labels
695+
696+
Use the `font` property to configure the `color`, `size`, and `family` of the label font.
697+
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.
698+
699+
```python
700+
import plotly.graph_objects as go
701+
702+
fig = go.Figure()
703+
704+
fig.add_shape(
705+
type="rect",
706+
x0=1,
707+
y0=1,
708+
x1=2,
709+
y1=3,
710+
label=dict(text="Text in rectangle", font=dict(color="red")),
711+
)
712+
fig.add_shape(
713+
type="line",
714+
x0=3,
715+
y0=0.5,
716+
x1=5,
717+
y1=0.8,
718+
line_width=3,
719+
label=dict(text="Text above line", font=dict(size=20)),
720+
)
721+
fig.add_shape(
722+
type="rect",
723+
x0=2.5,
724+
y0=2.5,
725+
x1=5,
726+
y1=3.5,
727+
label=dict(
728+
text="Text in rectangle 2", font=dict(family="Courier New, monospace", size=20)
729+
),
730+
)
731+
732+
fig.show()
733+
734+
```
735+
736+
### Setting Label Position
737+
738+
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`.
739+
740+
741+
```python
742+
import plotly.graph_objects as go
743+
744+
fig = go.Figure()
745+
746+
fig.add_shape(
747+
type="rect",
748+
x0=0,
749+
y0=0,
750+
x1=1.5,
751+
y1=1.5,
752+
label=dict(text="Text at middle center"),
753+
)
754+
755+
fig.add_shape(
756+
type="rect",
757+
x0=3,
758+
y0=0,
759+
x1=4.5,
760+
y1=1.5,
761+
label=dict(text="Text at top left", textposition="top left"),
762+
)
763+
764+
765+
fig.add_shape(
766+
type="line",
767+
x0=3,
768+
y0=2,
769+
x1=5,
770+
y1=3,
771+
line_width=3,
772+
label=dict(text="Text at start", textposition="start"),
773+
)
774+
775+
776+
fig.add_shape(
777+
type="line",
778+
x0=0,
779+
y0=2,
780+
x1=2,
781+
y1=3,
782+
line_width=3,
783+
label=dict(text="Text at middle"),
784+
)
785+
786+
fig.show()
787+
788+
```
789+
790+
### Setting Label Angle
791+
792+
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.
793+
794+
```python
795+
import plotly.graph_objects as go
796+
797+
fig = go.Figure()
798+
799+
fig.add_shape(
800+
type="rect",
801+
x0=0,
802+
y0=0,
803+
x1=1.5,
804+
y1=1.5,
805+
label=dict(text="Text at 45", textangle=45),
806+
)
807+
808+
fig.add_shape(
809+
type="rect",
810+
x0=3,
811+
y0=0,
812+
x1=4.5,
813+
y1=1.5,
814+
label=dict(text="Text at -45", textangle=-45),
815+
)
816+
817+
fig.show()
818+
819+
```
820+
821+
### Setting Label Padding
822+
823+
`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.
824+
825+
```python
826+
import plotly.graph_objects as go
827+
828+
fig = go.Figure()
829+
830+
831+
fig.add_shape(
832+
type="line",
833+
x0=3,
834+
y0=0,
835+
x1=5,
836+
y1=3,
837+
line_width=3,
838+
label=dict(text="Label padding of 30", textposition="start", padding=30),
839+
)
840+
841+
842+
fig.add_shape(
843+
type="line",
844+
x0=0,
845+
y0=0,
846+
x1=2,
847+
y1=3,
848+
line_width=3,
849+
label=dict(text="No label padding"),
850+
)
851+
852+
fig.show()
853+
854+
```
855+
668856
### Reference
669857
See https://plotly.com/python/reference/layout/shapes/ for more information and chart attribute options!

0 commit comments

Comments
 (0)