|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.1' |
| 9 | + jupytext_version: 1.2.1 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.7.3 |
| 24 | + plotly: |
| 25 | + description: How to use text template in Python with Plotly. |
| 26 | + display_as: file_settings |
| 27 | + has_thumbnail: true |
| 28 | + ipynb: ~notebook_demo/252 |
| 29 | + language: python |
| 30 | + layout: base |
| 31 | + name: Text Template |
| 32 | + order: 40 |
| 33 | + page_type: u-guide |
| 34 | + permalink: python/texttemplate/ |
| 35 | + thumbnail: thumbnail/texttemplate.jpg |
| 36 | + title: Text Template and Formatting| plotly |
| 37 | + v4upgrade: true |
| 38 | +--- |
| 39 | + |
| 40 | +### Add Text Template to Pie Chart |
| 41 | +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). |
| 42 | + |
| 43 | +```python |
| 44 | +import plotly.graph_objects as go |
| 45 | + |
| 46 | +fig = go.Figure(go.Pie( |
| 47 | + values = [2, 5, 3, 2.5], |
| 48 | + labels = ["R", "Python", "Java Script", "Matlab"], |
| 49 | + texttemplate = "%{label}: %{value} (%{percent})", |
| 50 | + textposition = "inside")) |
| 51 | + |
| 52 | +fig.show() |
| 53 | +``` |
| 54 | + |
| 55 | +### Customize Text Template |
| 56 | + |
| 57 | +The following example uses [textfont](https://plot.ly/python/reference/#scatterternary-textfont) to customize the added text. |
| 58 | + |
| 59 | +```python |
| 60 | +import plotly.graph_objects as go |
| 61 | + |
| 62 | +fig = go.Figure(go.Scatterternary( |
| 63 | + a = [3, 2, 5], |
| 64 | + b = [2, 5, 2], |
| 65 | + c = [5, 2, 2], |
| 66 | + mode = "markers+text", |
| 67 | + text = ["A", "B", "C"], |
| 68 | + texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})", |
| 69 | + textposition = "bottom center", |
| 70 | + textfont = {'family': "Times", 'size': [18, 21, 20], 'color': ["IndianRed", "MediumPurple", "DarkOrange"]} |
| 71 | +)) |
| 72 | + |
| 73 | +fig.show() |
| 74 | +``` |
| 75 | +### Set Date in Text Template |
| 76 | +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/). |
| 77 | + |
| 78 | +```python |
| 79 | +from plotly import graph_objects as go |
| 80 | + |
| 81 | +fig = go.Figure() |
| 82 | + |
| 83 | +fig.add_trace(go.Funnel( |
| 84 | + name = 'Montreal', |
| 85 | + orientation = "h", |
| 86 | + y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"], |
| 87 | + x = [100, 60, 40, 20], |
| 88 | + textposition = "inside", |
| 89 | + texttemplate = "%{label}")) |
| 90 | + |
| 91 | +fig.add_trace(go.Funnel( |
| 92 | + name = 'Vancouver', |
| 93 | + orientation = "h", |
| 94 | + y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"], |
| 95 | + x = [90, 70, 50, 10], |
| 96 | + textposition = "inside", |
| 97 | + textinfo = "label")) |
| 98 | + |
| 99 | +fig.update_layout(yaxis = {'type': 'date'}) |
| 100 | + |
| 101 | +fig.show() |
| 102 | +``` |
0 commit comments