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

texttemplate.py tutorial #151

merged 7 commits into from
Nov 2, 2019

Conversation

Mahdis-z
Copy link
Contributor

Doc upgrade checklist:

  • random seed is set if using random data
  • file has been moved from unconverted/x/y.md to x/y.md
  • old boilerplate at top and bottom of file has been removed
  • Every example is independently runnable and is optimized for short line count
  • no more plot() or iplot()
  • graph_objs has been renamed to graph_objects
  • fig = <something> call is high up in each example
  • minimal creation of intermediate trace objects
  • liberal use of add_trace and update_layout
  • fig.show() at the end of each example
  • px example at the top if appropriate
  • minimize usage of hex codes for colors in favour of those in https://github.com/plotly/plotly.py-docs/issues/14

@nicolaskruchten
Copy link
Contributor

@emmanuelle if you can take a look at this while I'm away that would be super helpful 🙏

@emmanuelle
Copy link
Contributor

ok !

v4upgrade: true
---

### Add Text Template to Pie Chart
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please switch to a more generic title: "Customizing displayed text with a Text Template"? The fact that it's a pie chart is not important here, you're introducing text templates, could be another chart.

Copy link
Contributor

@emmanuelle emmanuelle left a comment

Choose a reason for hiding this comment

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

I left a couple of comments (mostly I'm missing a few big-picture sentences about how to format these templates) but I hope it should be quick to address. Thanks!

@Mahdis-z
Copy link
Contributor Author

I left a couple of comments (mostly I'm missing a few big-picture sentences about how to format these templates) but I hope it should be quick to address. Thanks!

yeah right, we usually include links to the reference page to not duplicate what we already have in those pages.. but I did include some information to make it more interesting!

### 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) customize the tooltip text.
Copy link
Contributor

Choose a reason for hiding this comment

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

hovertemplate customizeS

Copy link
Contributor

@emmanuelle emmanuelle left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes! One more idea that I add: could you add one example with a plotly express figure and then call fig.update_traces to show that it's possible to update figures created with px?

We don't have texttemplate in px yet so fig.update_traces can't update it.

“mahdis-z” added 2 commits October 29, 2019 00:15
@emmanuelle
Copy link
Contributor

I'm not sure I understand why you removed the example with px? Was something wrong with it?

@nicolaskruchten
Copy link
Contributor

This probably belongs in https://plot.ly/python/text-and-annotations/ rather than as a new file.

y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
x = [100, 60, 40, 20],
textposition = "inside",
texttemplate = "%{label}"))
Copy link
Contributor

Choose a reason for hiding this comment

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

It would actually be interesting to use a different date format here to show how to customize the format. But I can't get it to work... Maybe I'll ping you on this @nicolaskruchten

Copy link
Contributor

Choose a reason for hiding this comment

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

So this could work for example
texttemplate = "%{y|%Y %b %-d}"

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's quite important to show an example with custom date formatting, because the formatting is quite specific

```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?


fig = go.Figure(go.Pie(
values = [40000000, 20000000, 30000000, 10000000],
labels = ["Sales", "Profit", "Income", "Expenses"],
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for this new example, the formatting looks really great! My only concern is that (if I'm not mistaken) usually you do not put sales profit income and expenses together on a bar chart because they are correlated (like profit is income minus expenses etc.). How about a pie chart about expenses : wages, operating expenses, cost of sales, advertising and promotion etc. (see for example https://canadianrestaurateur.wordpress.com/2012/05/28/how-to-compare-your-restaurant/#jp-carousel-184)

@emmanuelle
Copy link
Contributor

I left a tiny comment about labels in the pie chart but I don't need to review again, we're in 💃 land!

@Mahdis-z Mahdis-z merged commit d7c2b9f into master Nov 2, 2019
@Mahdis-z Mahdis-z deleted the py_treemap_ branch November 2, 2019 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants