Skip to content

Commit 0ae7596

Browse files
committed
Update shapes.md
1 parent 7a28a58 commit 0ae7596

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

Diff for: doc/python/shapes.md

+48-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.3'
9-
jupytext_version: 1.14.5
9+
jupytext_version: 1.14.6
1010
kernelspec:
1111
display_name: Python 3 (ipykernel)
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.8.8
23+
version: 3.10.10
2424
plotly:
2525
description: How to make SVG shapes in python. Examples of lines, circle, rectangle,
2626
and path.
@@ -677,12 +677,12 @@ import plotly.graph_objects as go
677677
fig = go.Figure()
678678

679679
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,
686686
label=dict(text="Text in rectangle")
687687
)
688688
fig.add_shape(
@@ -701,8 +701,8 @@ fig.show()
701701

702702
#### Styling Text Labels
703703

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.
706706

707707
```python
708708
import plotly.graph_objects as go
@@ -776,7 +776,7 @@ fig.add_shape(
776776

777777
fig.add_shape(
778778
type="line",
779-
line_color="MediumSlateBlue",
779+
line_color="MediumSlateBlue",
780780
x0=3,
781781
y0=2,
782782
x1=5,
@@ -870,10 +870,10 @@ fig.show()
870870

871871
#### Setting Label Anchors
872872

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.
874874
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.
875875

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.
877877

878878

879879
```python
@@ -917,18 +917,27 @@ fig.show()
917917

918918
*New in 5.15*
919919

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:
921921

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)
923930

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.
925932

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.
926934

927935
```python
928936
import plotly.graph_objects as go
929937

930938
fig = go.Figure()
931939

940+
932941
fig.add_shape(
933942
type="rect",
934943
fillcolor="MediumSlateBlue",
@@ -955,9 +964,12 @@ fig.add_shape(
955964
x0=3,
956965
y0=0.5,
957966
x1=5,
958-
y1=0.8,
967+
y1=1.5,
959968
line_width=3,
960-
label=dict(texttemplate="Slope: %{slope:.3f}", font=dict(size=20)),
969+
label=dict(
970+
texttemplate="Slope of %{slope:.3f} and length of %{length:.3f}",
971+
font=dict(size=20),
972+
),
961973
)
962974
fig.add_shape(
963975
type="rect",
@@ -980,23 +992,38 @@ fig.show()
980992

981993
*New in 5.15*
982994

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.
984998

985999
```python
9861000
import plotly.graph_objects as go
1001+
from plotly import data
1002+
1003+
df = data.stocks()
9871004

9881005
fig = go.Figure(
989-
layout=go.Layout(newshape=dict(label=dict(texttemplate="Slope: %{slope:.3f}")))
1006+
data=go.Scatter(
1007+
x=df.date,
1008+
y=df.GOOG,
1009+
),
1010+
layout=go.Layout(
1011+
yaxis=dict(title="Price in USD"),
1012+
newshape=dict(
1013+
label=dict(texttemplate="Change: %{dy:.2f}")
1014+
),
1015+
title="Google Share Price 2018/2019",
1016+
),
9901017
)
9911018

1019+
9921020
fig.show(
9931021
config={
9941022
"modeBarButtonsToAdd": [
9951023
"drawline",
9961024
]
9971025
}
9981026
)
999-
10001027
```
10011028

10021029
### Reference

0 commit comments

Comments
 (0)