You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/python/shapes.md
+31-7
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ jupyter:
20
20
name: python
21
21
nbconvert_exporter: python
22
22
pygments_lexer: ipython3
23
-
version: 3.10.8
23
+
version: 3.10.10
24
24
plotly:
25
25
description: How to make SVG shapes in python. Examples of lines, circle, rectangle,
26
26
and path.
@@ -917,18 +917,27 @@ fig.show()
917
917
918
918
*New in 5.15*
919
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. Add a variable with "%{variable}".
920
+
Use `texttemplate` to add text with variables to shapes. You have access to raw variables (`x0`, `x1`, `y0`, `y1`), which use raw data values from the shape definition, and the following calculated variables:
921
921
922
-
This example adds the raw variables `x0` and `y0` to a rectangle and shows the calculated variables `height`, `slope`, `length`, and `width` on three other shapes.
922
+
-`xcenter`: (x0 + x1) / 2
923
+
-`ycenter`: (y0 + y1) / 2
924
+
-`dx`: x1 - x0
925
+
-`dy`: y1 - y0
926
+
-`width`: abs(x1 - x0)
927
+
-`height`: abs(y1 - y0)
928
+
-`length` (for lines only): sqrt(dx^2 + dy^2)
929
+
-`slope`: (y1 - y0) / (x1 - x0)
923
930
924
-
For a complete list of available variables, see the [Shape Reference Docs](https://plotly.com/python/reference/layout/shapes/).
931
+
`texttemplate` supports d3 number and date formatting.
925
932
933
+
Add a variable with "%{variable}". This example adds the raw variables `x0` and `y0` to a rectangle and shows the calculated variables `height`, `slope`, `length`, and `width` on three other shapes.
926
934
927
935
```python
928
936
import plotly.graph_objects as go
929
937
930
938
fig = go.Figure()
931
939
940
+
932
941
fig.add_shape(
933
942
type="rect",
934
943
fillcolor="MediumSlateBlue",
@@ -983,23 +992,38 @@ fig.show()
983
992
984
993
*New in 5.15*
985
994
986
-
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.
995
+
You can also use `texttemplate` to add text with variables to new shapes drawn on the graph.
996
+
997
+
In this example, we enable drawing lines on the figure by adding `drawline` to `modeBarButtonsToAdd` in `config`. We then define a `texttemplate` for shapes that shows the calculated variable `dy`. Select **Draw line** in the modebar to try it out.
0 commit comments