Skip to content

Commit e5fdd7c

Browse files
committed
add text annotation example
1 parent 059433f commit e5fdd7c

File tree

1 file changed

+66
-18
lines changed

1 file changed

+66
-18
lines changed

Diff for: doc/python/text-and-annotations.md

+66-18
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.16.2
9+
jupytext_version: 1.16.3
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.10.0
23+
version: 3.10.14
2424
plotly:
2525
description: How to add text labels and annotations to plots in python.
2626
display_as: file_settings
@@ -131,7 +131,7 @@ Here is a bar chart with the default behavior which will scale down text to fit.
131131
import plotly.express as px
132132

133133
df = px.data.gapminder(year=2007)
134-
fig = px.bar(df, x='continent', y='pop', color="lifeExp", text='country',
134+
fig = px.bar(df, x='continent', y='pop', color="lifeExp", text='country',
135135
title="Default behavior: some text is tiny")
136136
fig.update_traces(textposition='inside')
137137
fig.show()
@@ -143,7 +143,7 @@ Here is the same figure with uniform text applied: the text for all bars is the
143143
import plotly.express as px
144144

145145
df = px.data.gapminder(year=2007)
146-
fig = px.bar(df, x='continent', y='pop', color="lifeExp", text='country',
146+
fig = px.bar(df, x='continent', y='pop', color="lifeExp", text='country',
147147
title="Uniform Text: min size is 8, hidden if can't fit")
148148
fig.update_traces(textposition='inside')
149149
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
@@ -448,7 +448,7 @@ fig.show()
448448

449449
## Text Case
450450

451-
**New in 5.23**
451+
*New in 5.23*
452452

453453
You can configure text case using the `textfont.textcase` property. In this example, we set `textfont.textcase="upper"` to transform the text on all bars to uppercase.
454454

@@ -462,17 +462,17 @@ grouped = df[df.year == 2007].loc[df[df.year == 2007].groupby('continent')['life
462462

463463
fig = go.Figure(
464464
data=go.Bar(
465-
x=grouped['lifeExp'],
466-
y=grouped['continent'],
465+
x=grouped['lifeExp'],
466+
y=grouped['continent'],
467467
text=grouped['country'],
468468
orientation='h',
469469
textfont=dict(
470470
family="sans serif",
471471
size=14,
472-
# Here we set textcase to "upper.
472+
# Here we set textcase to "upper.
473473
# Set to lower" for lowercase text, or "word caps" to capitalize the first letter of each word
474474
textcase="upper"
475-
475+
476476
)
477477
),
478478
layout=go.Layout(
@@ -486,9 +486,9 @@ fig.show()
486486

487487
## Text Lines
488488

489-
**New in 5.23**
489+
*New in 5.23*
490490

491-
You can add decoration lines to text using the `textfont.lineposition` property. This property accepts `"under"`, `"over"`, and `"through"`, or a combination of these separated by a `+`.
491+
You can add decoration lines to text using the `textfont.lineposition` property. This property accepts `"under"`, `"over"`, and `"through"`, or a combination of these separated by a `+`.
492492

493493
```python
494494
import plotly.graph_objects as go
@@ -500,12 +500,12 @@ grouped = df[df.year == 2002].loc[df[df.year == 2002].groupby('continent')['life
500500

501501
fig = go.Figure(
502502
data=go.Bar(
503-
x=grouped['lifeExp'],
504-
y=grouped['continent'],
503+
x=grouped['lifeExp'],
504+
y=grouped['continent'],
505505
text=grouped['country'],
506506
orientation='h',
507-
marker_color='MediumSlateBlue',
508-
textfont=dict(
507+
marker_color='MediumSlateBlue',
508+
textfont=dict(
509509
lineposition="under" # combine different line positions with a "+" to add more than one: "under+over"
510510
)
511511
),
@@ -520,7 +520,7 @@ fig.show()
520520

521521
## Text Shadow
522522

523-
**New in 5.23**
523+
*New in 5.23*
524524

525525
You can apply a shadow effect to text using the `textfont.shadow` property. This property accepts shadow specifications in the same format as the [text-shadow CSS property](https://developer.mozilla.org/en-US/docs/Web/CSS/text-shadow).
526526

@@ -534,8 +534,8 @@ grouped = df[df.year == 1997].loc[df[df.year == 1997].groupby('continent')['life
534534

535535
fig = go.Figure(
536536
data=go.Bar(
537-
x=grouped['lifeExp'],
538-
y=grouped['continent'],
537+
x=grouped['lifeExp'],
538+
y=grouped['continent'],
539539
text=grouped['country'],
540540
orientation='h',
541541
textfont=dict(
@@ -653,6 +653,54 @@ fig.update_layout(
653653
fig.show()
654654
```
655655

656+
### HTML Tags in Text
657+
658+
The `text` attribute supports the following HTML tags: `<br>`,`<b>`,`<a>`, `<em>`, `<sup>` and `<span>`.
659+
In version 5.23 and later, `<s>` and `<u>`are also supported.
660+
661+
```python
662+
import plotly.graph_objects as go
663+
664+
fig = go.Figure(
665+
data=[
666+
go.Scatter(
667+
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
668+
y=[0, 1, 3, 2, 4, 3, 4, 6, 5],
669+
mode="lines+markers",
670+
name="Series 1",
671+
),
672+
go.Scatter(
673+
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
674+
y=[0, 4, 5, 1, 2, 2, 3, 4, 2],
675+
mode="lines+markers",
676+
name="Series 2",
677+
),
678+
],
679+
layout=go.Layout(
680+
annotations=[
681+
dict(
682+
x=2,
683+
y=5,
684+
text="Text annotation using <b>bolded text</b>, <i>italicized text</i>, <u>underlined text</u>, <br>and a new line",
685+
showarrow=True,
686+
arrowhead=1,
687+
),
688+
dict(
689+
x=4,
690+
y=4,
691+
text="Text annotation with <a href='https://dash.plotly.com'>a link</a>.",
692+
showarrow=False,
693+
yshift=10,
694+
),
695+
],
696+
showlegend=False,
697+
),
698+
)
699+
700+
fig.show()
701+
702+
```
703+
656704
### Positioning Text Annotations Absolutely
657705

658706
By default, text annotations have `xref` and `yref` set to `"x"` and `"y"`, respectively, meaning that their x/y coordinates are with respect to the axes of the plot. This means that panning the plot will cause the annotations to move. Setting `xref` and/or `yref` to `"paper"` will cause the `x` and `y` attributes to be interpreted in [paper coordinates](/python/figure-structure/#positioning-with-paper-container-coordinates-or-axis-domain-coordinates).

0 commit comments

Comments
 (0)