Skip to content

texttemplate.py tutorial #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions python/text-and-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.1'
jupytext_version: 1.1.7
jupytext_version: 1.2.1
kernelspec:
display_name: Python 3
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.5
version: 3.7.3
plotly:
description: How to add text labels and annotations to plots in python.
display_as: file_settings
Expand Down Expand Up @@ -512,5 +512,73 @@ fig.update_layout(
fig.show()
```


### Customize Displayed Text with a Text Template
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).
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).
`texttemplate` customizes the text that appears on your plot vs. [hovertemplate](https://plot.ly/python/reference/#pie-hovertemplate) that customizes the tooltip text.

```python
import plotly.graph_objects as go

fig = go.Figure(go.Pie(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolaskruchten I think this is the pie chart example you were asking for, just using programming languages instead of revenue. Would you prefer having a more business-oriented example with dollars? (with the $ symbol in the template). @Mahdis-z you can give it a try with revenue by activity sector for example

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I chatted with Nicolas and he pointed out we would need an example of something which we cannot do with textinfo, such as in the pie chart displaying the values as, for example, formatting 12 000 000 as $ 12M. @Mahdis-z could you please add such an example after the current pie chart example?

values = [40000000, 20000000, 30000000, 10000000],
labels = ["Wages", "Operating expenses", "Cost of sales", "Insurance"],
texttemplate = "%{label}: %{value:$,s} <br>(%{percent})",
textposition = "inside"))

fig.show()
```

### Customize Text Template

The following example uses [textfont](https://plot.ly/python/reference/#scatterternary-textfont) to customize the added text.

```python
import plotly.graph_objects as go

fig = go.Figure(go.Scatterternary(
a = [3, 2, 5],
b = [2, 5, 2],
c = [5, 2, 2],
mode = "markers+text",
text = ["A", "B", "C"],
texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
textposition = "bottom center",
textfont = {'family': "Times", 'size': [18, 21, 20], 'color': ["IndianRed", "MediumPurple", "DarkOrange"]}
))

fig.show()
```
### Set Date in Text Template
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/).
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.

```python
from plotly import graph_objects as go

fig = go.Figure()

fig.add_trace(go.Funnel(
name = 'Montreal',
orientation = "h",
y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
x = [100, 60, 40, 20],
textposition = "inside",
texttemplate = "%{y| %a. %_d %b %Y}"))

fig.add_trace(go.Funnel(
name = 'Vancouver',
orientation = "h",
y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
x = [90, 70, 50, 10],
textposition = "inside",
textinfo = "label"))

fig.update_layout(yaxis = {'type': 'date'})

fig.show()
```

#### Reference
See https://plot.ly/python/reference/#layout-annotations for more information and chart attribute options!