Skip to content

Commit d7c2b9f

Browse files
authored
Merge pull request #151 from plotly/py_treemap_
texttemplate.py tutorial
2 parents e078df4 + 921ca0b commit d7c2b9f

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

python/text-and-annotations.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.1'
9-
jupytext_version: 1.1.7
9+
jupytext_version: 1.2.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.5
23+
version: 3.7.3
2424
plotly:
2525
description: How to add text labels and annotations to plots in python.
2626
display_as: file_settings
@@ -512,5 +512,73 @@ fig.update_layout(
512512
fig.show()
513513
```
514514

515+
516+
### Customize Displayed Text with a Text Template
517+
To show an arbitrary text in your chart you can use [texttemplate](https://plot.ly/python/reference/#pie-texttemplate), which is a template string used for rendering the information, and will override [textinfo](https://plot.ly/python/reference/#treemap-textinfo).
518+
This template string can include `variables` in %{variable} format, `numbers` in [d3-format's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_forma), and `date` in [d3-time-fomrat's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Formatting.md#format).
519+
`texttemplate` customizes the text that appears on your plot vs. [hovertemplate](https://plot.ly/python/reference/#pie-hovertemplate) that customizes the tooltip text.
520+
521+
```python
522+
import plotly.graph_objects as go
523+
524+
fig = go.Figure(go.Pie(
525+
values = [40000000, 20000000, 30000000, 10000000],
526+
labels = ["Wages", "Operating expenses", "Cost of sales", "Insurance"],
527+
texttemplate = "%{label}: %{value:$,s} <br>(%{percent})",
528+
textposition = "inside"))
529+
530+
fig.show()
531+
```
532+
533+
### Customize Text Template
534+
535+
The following example uses [textfont](https://plot.ly/python/reference/#scatterternary-textfont) to customize the added text.
536+
537+
```python
538+
import plotly.graph_objects as go
539+
540+
fig = go.Figure(go.Scatterternary(
541+
a = [3, 2, 5],
542+
b = [2, 5, 2],
543+
c = [5, 2, 2],
544+
mode = "markers+text",
545+
text = ["A", "B", "C"],
546+
texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
547+
textposition = "bottom center",
548+
textfont = {'family': "Times", 'size': [18, 21, 20], 'color': ["IndianRed", "MediumPurple", "DarkOrange"]}
549+
))
550+
551+
fig.show()
552+
```
553+
### Set Date in Text Template
554+
The following example shows how to show date by setting [axis.type](https://plot.ly/python/reference/#layout-yaxis-type) in [funnel charts](https://plot.ly/python/funnel-charts/).
555+
As you can see [textinfo](https://plot.ly/python/reference/#funnel-textinfo) and [texttemplate](https://plot.ly/python/reference/#funnel-texttemplate) have the same functionality when you want to determine 'just' the trace information on the graph.
556+
557+
```python
558+
from plotly import graph_objects as go
559+
560+
fig = go.Figure()
561+
562+
fig.add_trace(go.Funnel(
563+
name = 'Montreal',
564+
orientation = "h",
565+
y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
566+
x = [100, 60, 40, 20],
567+
textposition = "inside",
568+
texttemplate = "%{y| %a. %_d %b %Y}"))
569+
570+
fig.add_trace(go.Funnel(
571+
name = 'Vancouver',
572+
orientation = "h",
573+
y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
574+
x = [90, 70, 50, 10],
575+
textposition = "inside",
576+
textinfo = "label"))
577+
578+
fig.update_layout(yaxis = {'type': 'date'})
579+
580+
fig.show()
581+
```
582+
515583
#### Reference
516584
See https://plot.ly/python/reference/#layout-annotations for more information and chart attribute options!

0 commit comments

Comments
 (0)