Skip to content

Commit 12a1cf6

Browse files
Merge pull request #3514 from plotly/dec-new-features-docs
New features docs - Dec
2 parents af4dc37 + c19b99a commit 12a1cf6

File tree

5 files changed

+180
-24
lines changed

5 files changed

+180
-24
lines changed

Diff for: doc/python/2D-Histogram.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.3.1
8+
format_version: '1.3'
9+
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.8
23+
version: 3.7.11
2424
plotly:
2525
description: How to make 2D Histograms in Python with Plotly.
2626
display_as: statistical
@@ -82,6 +82,17 @@ fig = px.density_heatmap(df, x="total_bill", y="tip", facet_row="sex", facet_col
8282
fig.show()
8383
```
8484

85+
You can add the `z` values as text to 2D Histogram points using `fig.update_traces(texttemplate="%{z}")`
86+
87+
```python
88+
import plotly.express as px
89+
df = px.data.tips()
90+
91+
fig = px.density_heatmap(df, x="total_bill", y="tip")
92+
fig.update_traces(texttemplate="%{z}")
93+
fig.show()
94+
```
95+
8596
### Other aggregation functions than `count`
8697

8798
By passing in a `z` value and a `histfunc`, density heatmaps can perform basic aggregation operations. Here we show average Sepal Length grouped by Petal Length and Petal Width for the Iris dataset.
@@ -235,5 +246,24 @@ fig.update_layout(
235246
fig.show()
236247
```
237248

249+
### Text on 2D Histogram Points
250+
251+
In this example we add text to 2D Histogram points. We use the values from the `z` attribute for the text.
252+
253+
```python
254+
import plotly.graph_objects as go
255+
from plotly import data
256+
257+
df = data.tips()
258+
259+
fig = go.Figure(go.Histogram2d(
260+
x=df.total_bill,
261+
y=df.tip,
262+
texttemplate= "%{z}"
263+
))
264+
265+
fig.show()
266+
```
267+
238268
#### Reference
239269
See https://plotly.com/python/reference/histogram2d/ for more information and chart attribute options!

Diff for: doc/python/annotated-heatmap.md

+45-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.3'
9+
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.11
2424
plotly:
2525
description: How to make Annotated Heatmaps in Python with Plotly.
2626
display_as: scientific
@@ -34,9 +34,48 @@ jupyter:
3434
thumbnail: thumbnail/ann_heat.jpg
3535
---
3636

37-
#### Simple Annotated Heatmap
37+
### Annotated Heatmaps with plotly.express and px.imshow
38+
39+
These examples use [px.imshow](/python/imshow) to create Annotated Heatmaps. px.imshow is the recommended way to create heatmaps with z-annotations.
40+
41+
42+
#### Basic Annotated Heatmap for z-annotations
43+
44+
After creating a figure with `px.imshow`, you can add z-annotations with `.update_traces(texttemplate="%{z}")`.
45+
46+
```python
47+
import plotly.express as px
48+
49+
df = px.data.medals_wide(indexed=True)
50+
51+
fig = px.imshow(df)
52+
fig.update_traces(texttemplate="%{z}")
53+
54+
fig.show()
55+
```
56+
57+
#### Custom Font
3858

39-
This page details the use of a [figure factory](/python/figure-factories/). For more examples with Heatmaps, see [this page](/python/heatmaps/).
59+
You can make changes to the font using `textfont`. Here we set the font size to 20.
60+
61+
```python
62+
import plotly.express as px
63+
64+
df = px.data.medals_wide(indexed=True)
65+
66+
fig = px.imshow(df)
67+
fig.update_traces(texttemplate="%{z}")
68+
fig.update_traces(textfont={"size":20})
69+
70+
fig.show()
71+
```
72+
73+
### Annotated Heatmaps with [figure factory](/python/figure-factories/). For more examples with Heatmaps, see [this page](/python/heatmaps/).
74+
75+
The remaining examples show how to create Annotated Heatmaps with [figure factory](/python/figure-factories/). For more examples with Heatmaps, see [this page](/python/heatmaps/).
76+
77+
78+
#### Simple Annotated Heatmap
4079

4180
```python
4281
import plotly.figure_factory as ff

Diff for: doc/python/colorscales.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.6.0
8+
format_version: '1.3'
9+
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,10 +20,10 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.6
23+
version: 3.7.11
2424
plotly:
25-
description: How to set, create and control continuous color scales and color bars
26-
in scatter, bar, map and heatmap figures.
25+
description: How to set, create and control continuous color scales and color
26+
bars in scatter, bar, map and heatmap figures.
2727
display_as: file_settings
2828
has_thumbnail: true
2929
ipynb: ~notebook_demo/187
@@ -518,6 +518,31 @@ fig.add_trace(go.Heatmap(
518518
fig.show()
519519
```
520520

521+
### Color Bar Displayed Horizontally
522+
523+
By default, color bars are displayed vertically. You can change a color bar to be displayed horizontally by setting the `colorbar` `orientation` attribute to `h`.
524+
525+
```python
526+
import plotly.graph_objects as go
527+
528+
import six.moves.urllib
529+
import json
530+
531+
# Load heatmap data
532+
response = six.moves.urllib.request.urlopen(
533+
"https://raw.githubusercontent.com/plotly/datasets/master/custom_heatmap_colorscale.json")
534+
dataset = json.load(response)
535+
536+
# Create and show figure
537+
fig = go.Figure()
538+
539+
fig.add_trace(go.Heatmap(
540+
z=dataset["z"],
541+
colorbar=dict(orientation='h')))
542+
543+
fig.show()
544+
```
545+
521546
### Sharing a Color Axis with Graph Objects
522547

523548
To share colorscale information in multiple subplots, you can use [coloraxis](https://plotly.com/javascript/reference/scatter/#scatter-marker-line-coloraxis).

Diff for: doc/python/heatmaps.md

+39-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.6.0
8+
format_version: '1.3'
9+
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.6
23+
version: 3.7.11
2424
plotly:
2525
description: How to make Heatmaps in Python with Plotly.
2626
display_as: scientific
@@ -86,6 +86,21 @@ fig.update_xaxes(side="top")
8686
fig.show()
8787
```
8888

89+
### Adding and customizing text on points
90+
91+
You can add text to heatmap points with `.update_traces(texttemplate="%{variable}")`. Here we use the values of the `z` attribute for `variable`. We also customize the font size with `textfont`.
92+
93+
```python
94+
import plotly.express as px
95+
96+
df = px.data.medals_wide(indexed=True)
97+
fig = px.imshow(df)
98+
fig.update_traces(texttemplate="%{z}")
99+
fig.update_traces(textfont={"size":20})
100+
101+
fig.show()
102+
```
103+
89104
### Basic Heatmap with `plotly.graph_objects`
90105

91106
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Heatmap` class from `plotly.graph_objects`](/python/graph-objects/).
@@ -197,6 +212,26 @@ fig.update_layout(
197212
fig.show()
198213
```
199214

215+
### Text on Heatmap Points
216+
217+
In this example we add text to heatmap points using `texttemplate`. We use the values from the `text` attribute for the text. We also adjust the font size using `textfont`.
218+
219+
```python
220+
import plotly.graph_objects as go
221+
222+
fig = go.Figure(data=go.Heatmap(
223+
z=[[1, 20, 30],
224+
[20, 1, 60],
225+
[30, 60, 1]],
226+
text=[['one', 'twenty', 'thirty'],
227+
['twenty', 'one', 'sixty'],
228+
['thirty', 'sixty', 'one']],
229+
texttemplate="%{text}",
230+
textfont={"size":20}))
231+
232+
fig.show()
233+
```
234+
200235
### Heatmap and datashader
201236

202237
Arrays of rasterized values build by datashader can be visualized using

Diff for: doc/python/histograms.md

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.4.2
8+
format_version: '1.3'
9+
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.7
23+
version: 3.7.11
2424
plotly:
2525
description: How to make Histograms in Python with Plotly.
2626
display_as: statistical
@@ -208,6 +208,18 @@ fig = px.histogram(df, x="total_bill", color="sex", marginal="rug", # can be `bo
208208
fig.show()
209209
```
210210

211+
### Adding bar text
212+
213+
You can add text to histogram bars using `fig.update_traces(texttemplate="%{variable}")`, where `variable` is the text to add. In this example, we add the y-axis values to the bars.
214+
215+
```python
216+
import plotly.express as px
217+
df = px.data.tips()
218+
fig = px.histogram(df, x="total_bill", y="tip", histfunc='avg', nbins=8)
219+
fig.update_traces(texttemplate="%{y}")
220+
fig.show()
221+
```
222+
211223
## Histograms with go.Histogram
212224

213225
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Histogram` class from `plotly.graph_objects`](/python/graph-objects/). All of the available histogram options are described in the histogram section of the reference page: https://plotly.com/python/reference#histogram.
@@ -340,6 +352,21 @@ fig.update_layout(
340352
fig.show()
341353
```
342354

355+
### Histogram Bar Text
356+
357+
You can add text to histogram bars using the `texttemplate` argument. In this example we add the x-axis values as text following the format `%{variable}`. We also adjust the size of the text using `textfont_size`.
358+
359+
```python
360+
import plotly.graph_objects as go
361+
362+
numbers = ["5", "10", "3", "10", "5", "8", "5", "5"]
363+
364+
fig = go.Figure()
365+
fig.add_trace(go.Histogram(x=numbers, name="count", texttemplate="%{x}", textfont_size=20))
366+
367+
fig.show()
368+
```
369+
343370
### Cumulative Histogram
344371

345372
```python

0 commit comments

Comments
 (0)