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
+48-21
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ jupyter:
6
6
extension: .md
7
7
format_name: markdown
8
8
format_version: '1.3'
9
-
jupytext_version: 1.14.5
9
+
jupytext_version: 1.14.6
10
10
kernelspec:
11
11
display_name: Python 3 (ipykernel)
12
12
language: python
@@ -20,7 +20,7 @@ jupyter:
20
20
name: python
21
21
nbconvert_exporter: python
22
22
pygments_lexer: ipython3
23
-
version: 3.8.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.
@@ -677,12 +677,12 @@ import plotly.graph_objects as go
677
677
fig = go.Figure()
678
678
679
679
fig.add_shape(
680
-
type="rect",
681
-
fillcolor='turquoise',
682
-
x0=1,
683
-
y0=1,
684
-
x1=2,
685
-
y1=3,
680
+
type="rect",
681
+
fillcolor='turquoise',
682
+
x0=1,
683
+
y0=1,
684
+
x1=2,
685
+
y1=3,
686
686
label=dict(text="Text in rectangle")
687
687
)
688
688
fig.add_shape(
@@ -701,8 +701,8 @@ fig.show()
701
701
702
702
#### Styling Text Labels
703
703
704
-
Use the `font` property to configure the `color`, `size`, and `family` of the label font.
705
-
In this example, we change the label color of the first rectangle to "DarkOrange", 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.
704
+
Use the `font` property to configure the `color`, `size`, and `family` of the label font.
705
+
In this example, we change the label color of the first rectangle to "DarkOrange", 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.
706
706
707
707
```python
708
708
import plotly.graph_objects as go
@@ -776,7 +776,7 @@ fig.add_shape(
776
776
777
777
fig.add_shape(
778
778
type="line",
779
-
line_color="MediumSlateBlue",
779
+
line_color="MediumSlateBlue",
780
780
x0=3,
781
781
y0=2,
782
782
x1=5,
@@ -870,10 +870,10 @@ fig.show()
870
870
871
871
#### Setting Label Anchors
872
872
873
-
`xanchor` sets a label's horizontal positional anchor and `yanchor` sets its vertical position anchor.
873
+
`xanchor` sets a label's horizontal positional anchor and `yanchor` sets its vertical position anchor.
874
874
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.
875
875
876
-
In this example, `yanchor`is set to "top", instead of the default of "bottom" for lines, meaning the text displays below the line.
876
+
In this example, `yanchor`is set to "top", instead of the default of "bottom" for lines, meaning the text displays below the line.
877
877
878
878
879
879
```python
@@ -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`, 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.
texttemplate="Slope of %{slope:.3f} and length of %{length:.3f}",
971
+
font=dict(size=20),
972
+
),
961
973
)
962
974
fig.add_shape(
963
975
type="rect",
@@ -980,23 +992,38 @@ fig.show()
980
992
981
993
*New in 5.15*
982
994
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.
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