Skip to content

Commit f04ebfc

Browse files
committed
Update shapes.md
1 parent 77d0211 commit f04ebfc

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

doc/python/shapes.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.10.9
23+
version: 3.8.8
2424
plotly:
2525
description: How to make SVG shapes in python. Examples of lines, circle, rectangle,
2626
and path.
@@ -913,5 +913,91 @@ fig.add_shape(
913913
fig.show()
914914
```
915915

916+
#### Variables in Shape Label Text
917+
918+
*New in 5.15*
919+
920+
Use `texttemplate` to add text with variables to shapes. `texttemplate` uses d3 number and date formatting and supports raw variables, which use the raw data from the shape definition, and some calculated variables.
921+
922+
This example adds the raw variables `x0` and `y0` to a rectangle and shows the calculated variables `height`, `slope`, and `width`on three other shapes.
923+
924+
For a complete list of available variables, see the [Shape Reference Docs](https://plotly.com/python/reference/layout/shapes/).
925+
926+
927+
```python
928+
import plotly.graph_objects as go
929+
930+
fig = go.Figure()
931+
932+
fig.add_shape(
933+
type="rect",
934+
fillcolor="MediumSlateBlue",
935+
x0=-0.5,
936+
y0=-0.5,
937+
x1=1,
938+
y1=1,
939+
label=dict(
940+
texttemplate="x0 is %{x0:.3f}, y0 is %{y0:.3f}", font=dict(color="DarkOrange")
941+
),
942+
)
943+
944+
fig.add_shape(
945+
type="rect",
946+
fillcolor="MediumSlateBlue",
947+
x0=1,
948+
y0=1,
949+
x1=2,
950+
y1=3,
951+
label=dict(texttemplate="Height: %{height:.3f}", font=dict(color="DarkOrange")),
952+
)
953+
fig.add_shape(
954+
type="line",
955+
x0=3,
956+
y0=0.5,
957+
x1=5,
958+
y1=0.8,
959+
line_width=3,
960+
label=dict(texttemplate="Slope: %{slope:.3f}", font=dict(size=20)),
961+
)
962+
fig.add_shape(
963+
type="rect",
964+
fillcolor="Lavender",
965+
x0=2.5,
966+
y0=2.5,
967+
x1=5,
968+
y1=3.5,
969+
label=dict(
970+
texttemplate="Width: %{width:.3f}",
971+
font=dict(family="Courier New, monospace", size=20),
972+
),
973+
)
974+
975+
fig.show()
976+
977+
```
978+
979+
#### Variables in Shape Label Text for New Shapes
980+
981+
*New in 5.15*
982+
983+
Use `texttemplate` to add text with variables to new shapes drawn on the graph. This example figure is configured to allow the user to draw lines and automatically labels each line with its slope. Select **Draw line** in the modebar to try it out.
984+
985+
```python
986+
import plotly.graph_objects as go
987+
988+
fig = go.Figure(
989+
layout=go.Layout(newshape=dict(label=dict(texttemplate="Slope: %{slope:.3f}")))
990+
)
991+
992+
fig.show(
993+
config={
994+
"modeBarButtonsToAdd": [
995+
"drawline",
996+
]
997+
}
998+
)
999+
1000+
```
1001+
9161002
### Reference
9171003
See https://plotly.com/python/reference/layout/shapes/ for more information and chart attribute options!

0 commit comments

Comments
 (0)