Skip to content

Commit b729e33

Browse files
committed
Update shapes.md
1 parent e430257 commit b729e33

File tree

1 file changed

+91
-12
lines changed

1 file changed

+91
-12
lines changed

doc/python/shapes.md

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -930,7 +930,7 @@ Use `texttemplate` to add text with variables to shapes. You have access to raw
930930

931931
`texttemplate` supports d3 number and date formatting.
932932

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

935935
```python
936936
import plotly.graph_objects as go
@@ -1017,6 +1017,85 @@ fig = go.Figure(
10171017
)
10181018

10191019

1020+
fig.show(
1021+
config={
1022+
"modeBarButtonsToAdd": [
1023+
"drawline",
1024+
]
1025+
}
1026+
)
1027+
```
1028+
1029+
#### Shapes in the Legend
1030+
1031+
*New in 5.16*
1032+
1033+
You can add a shape to the legend by setting `showlegend=True` on the shape. In this example, we add the second shape to the legend. The name that appears for the shape in the legend is the shape's `name` if it is provided. If no `name` is provided, the shape label's `text` is used. If neither is provided, the legend item appears as "shape \<shape number>". For example, "shape 1".
1034+
1035+
```python
1036+
import plotly.express as px
1037+
1038+
df = px.data.stocks(indexed=True)
1039+
1040+
fig = px.line(df)
1041+
1042+
fig.add_shape(
1043+
type="rect",
1044+
x0="2018-09-24",
1045+
y0=0,
1046+
x1="2018-12-18",
1047+
y1=3,
1048+
line_width=0,
1049+
label=dict(text="Decline", textposition="top center", font=dict(size=20)),
1050+
fillcolor="green",
1051+
opacity=0.25,
1052+
)
1053+
1054+
fig.add_shape(
1055+
showlegend=True,
1056+
type="line",
1057+
x0=min(df.index),
1058+
y0=1,
1059+
x1=max(df.index),
1060+
y1=1,
1061+
line_width=3,
1062+
line_dash="dot",
1063+
label=dict(
1064+
text="Jan 1 2018 Baseline",
1065+
textposition="end",
1066+
font=dict(size=20, color="blue"),
1067+
yanchor="top",
1068+
),
1069+
)
1070+
1071+
fig.show()
1072+
```
1073+
1074+
`newshape` also supports `showlegend`. In this example, each new line drawn on the graph appears in the legend.
1075+
1076+
```python
1077+
import plotly.graph_objects as go
1078+
from plotly import data
1079+
1080+
df = data.stocks()
1081+
1082+
fig = go.Figure(
1083+
data=go.Scatter(
1084+
x=df.date,
1085+
y=df.AAPL,
1086+
name="Apple"
1087+
),
1088+
layout=go.Layout(
1089+
yaxis=dict(title="Price in USD"),
1090+
newshape=dict(
1091+
showlegend=True,
1092+
label=dict(texttemplate="Change: %{dy:.2f}")
1093+
),
1094+
title="Apple Share Price 2018/2019",
1095+
),
1096+
)
1097+
1098+
10201099
fig.show(
10211100
config={
10221101
"modeBarButtonsToAdd": [

0 commit comments

Comments
 (0)