Skip to content

Commit 9a9a67c

Browse files
Merge branch 'master' into master
2 parents be399fa + 6a023af commit 9a9a67c

24 files changed

+599
-106
lines changed

Diff for: CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Fixed
88
- Fixed ValueError when `ff.create_annotated_heatmap` passes `rgba()` colors into `to_rgb_color_list` [#3478](https://github.com/plotly/plotly.py/issues/3478)
99

10+
### Added
11+
12+
- `text_auto` argument to `px.bar`, `px.histogram`, `px.density_heatmap`, `px.imshow` [#3518](https://github.com/plotly/plotly.py/issues/3518)
13+
- Deprecated `ff.create_annotated_heatmap`, `ff.create_county_choropleth`, `ff.create_gantt` [#3518](https://github.com/plotly/plotly.py/issues/3518)
14+
1015
### Updated
11-
- Updated Plotly.js to from version 2.6.3 to version 2.8.0. See the [plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#280----2021-12-10) for more information. Notable changes include:
16+
- Updated Plotly.js to from version 2.6.3 to version 2.8.1. See the [plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#280----2021-12-10) for more information. Notable changes include:
1217
- Horizontal color bars
1318
- texttemplate for histogram-like traces
1419
- text for heatmap-like traces

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

+36-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ 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:
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.8
23+
version: 3.8.11
2424
plotly:
2525
description: How to make 2D Histograms in Python with Plotly.
2626
display_as: statistical
@@ -82,6 +82,20 @@ fig = px.density_heatmap(df, x="total_bill", y="tip", facet_row="sex", facet_col
8282
fig.show()
8383
```
8484

85+
### Displaying Text
86+
87+
*New in v5.5*
88+
89+
You can add the `z` values as text using the `text_auto` argument. Setting it to `True` will display the values on the bars, and setting it to a `d3-format` formatting string will control the output format.
90+
91+
```python
92+
import plotly.express as px
93+
df = px.data.tips()
94+
95+
fig = px.density_heatmap(df, x="total_bill", y="tip", text_auto=True)
96+
fig.show()
97+
```
98+
8599
### Other aggregation functions than `count`
86100

87101
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 +249,24 @@ fig.update_layout(
235249
fig.show()
236250
```
237251

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

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

+100-46
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ 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:
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.7
23+
version: 3.8.11
2424
plotly:
2525
description: How to make Annotated Heatmaps in Python with Plotly.
2626
display_as: scientific
@@ -34,56 +34,55 @@ jupyter:
3434
thumbnail: thumbnail/ann_heat.jpg
3535
---
3636

37-
#### Simple Annotated Heatmap
37+
### Annotated Heatmaps with Plotly Express
38+
39+
*New in v5.5*
40+
41+
As of version 5.5.0 of `plotly`, the **recommended way to [display annotated heatmaps is to use `px.imshow()`](/python/heatmaps/)** rather than the now-deprecated `create_annotated_heatmap` figure factory documented below for historical reasons.
42+
43+
44+
#### Basic Annotated Heatmap for z-annotations
3845

39-
This page details the use of a [figure factory](/python/figure-factories/). For more examples with Heatmaps, see [this page](/python/heatmaps/).
46+
After creating a figure with `px.imshow`, you can add z-annotations with `.update_traces(texttemplate="%{z}")`.
4047

4148
```python
42-
import plotly.figure_factory as ff
49+
import plotly.express as px
4350

4451
z = [[.1, .3, .5, .7, .9],
4552
[1, .8, .6, .4, .2],
4653
[.2, 0, .5, .7, .9],
4754
[.9, .8, .4, .2, 0],
4855
[.3, .4, .5, .7, 1]]
4956

50-
fig = ff.create_annotated_heatmap(z)
57+
fig = px.imshow(z, text_auto=True)
5158
fig.show()
5259
```
5360

54-
#### Defined Colorscale
61+
### Deprecated Figure Factory
5562

56-
```python
57-
import plotly.figure_factory as ff
63+
The remaining examples show how to create Annotated Heatmaps with the deprecated `create_annotated_heatmap` [figure factory](/python/figure-factories/).
5864

59-
z = [[.1, .3, .5, .7],
60-
[1, .8, .6, .4],
61-
[.6, .4, .2, .0],
62-
[.9, .7, .5, .3]]
6365

64-
fig = ff.create_annotated_heatmap(z, colorscale='Viridis')
65-
fig.show()
66-
```
67-
68-
#### Custom Colorscale
66+
#### Simple Annotated Heatmap
6967

7068
```python
7169
import plotly.figure_factory as ff
7270

73-
z = [[.1, .3, .5, .7],
74-
[1.0, .8, .6, .4],
75-
[.6, .4, .2, 0.0],
76-
[.9, .7, .5, .3]]
71+
z = [[.1, .3, .5, .7, .9],
72+
[1, .8, .6, .4, .2],
73+
[.2, 0, .5, .7, .9],
74+
[.9, .8, .4, .2, 0],
75+
[.3, .4, .5, .7, 1]]
7776

78-
colorscale = [[0, 'navy'], [1, 'plum']]
79-
font_colors = ['white', 'black']
80-
fig = ff.create_annotated_heatmap(z, colorscale=colorscale, font_colors=font_colors)
77+
fig = ff.create_annotated_heatmap(z)
8178
fig.show()
8279
```
8380

8481
#### Custom Text and X & Y Labels
8582
set `annotation_text` to a matrix with the same dimensions as `z`
8683

84+
> WARNING: this legacy figure factory requires the `y` array to be provided in reverse order, and will map the `z_text` to the `z` values in reverse order. **The use of the `px.imshow()` version below is highly recommended**
85+
8786
```python
8887
import plotly.figure_factory as ff
8988

@@ -102,6 +101,28 @@ fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscal
102101
fig.show()
103102
```
104103

104+
Here is the same figure using `px.imshow()`
105+
106+
```python
107+
import plotly.express as px
108+
109+
x = ['Team A', 'Team B', 'Team C']
110+
y = ['Game One', 'Game Two', 'Game Three']
111+
112+
z = [[.1, .3, .5],
113+
[1.0, .8, .6],
114+
[.6, .4, .2]]
115+
116+
z_text = [['Win', 'Lose', 'Win'],
117+
['Lose', 'Lose', 'Win'],
118+
['Win', 'Win', 'Lose']]
119+
120+
fig = px.imshow(z, x=x, y=y, color_continuous_scale='Viridis', aspect="auto")
121+
fig.update_traces(text=z_text, texttemplate="%{text}")
122+
fig.update_xaxes(side="top")
123+
fig.show()
124+
```
125+
105126
#### Annotated Heatmap with numpy
106127

107128
```python
@@ -122,10 +143,23 @@ for i in range(len(fig.layout.annotations)):
122143
fig.show()
123144
```
124145

125-
#### Custom Hovertext
146+
Here is the same figure using `px.imshow()`
147+
148+
```python
149+
import plotly.express as px
150+
import numpy as np
151+
np.random.seed(1)
152+
153+
z = np.random.randn(20, 20)
154+
155+
fig = px.imshow(z, text_auto=".2f", color_continuous_scale='Greys', aspect="auto")
156+
fig.show()
157+
```
158+
159+
Here is a fairly contrived example showing how one can display a periodic table with custom text and hover using `ff.create_annotated_heatmap()` (scroll below to see the `px.imshow()` equivalent).
126160

127161
```python
128-
# Add Periodic Table Data
162+
# Periodic Table Data
129163
symbol = [['H', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'He'],
130164
['Li', 'Be', '', '', '', '', '', '', '', '', '', '', 'B', 'C', 'N', 'O', 'F', 'Ne'],
131165
['Na', 'Mg', '', '', '', '', '', '', '', '', '', '', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar'],
@@ -141,11 +175,11 @@ symbol = [['H', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
141175

142176
element = [['Hydrogen', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Helium'],
143177
['Lithium', 'Beryllium', '', '', '', '', '', '', '', '', '', '', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon'],
144-
['Sodium', 'Magnesium', '', '', '', '', '', '', '', '', '', '', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', ' Argon'],
145-
['Potassium', ' Calcium', ' Scandium', ' Titanium', ' Vanadium', ' Chromium', 'Manganese', 'Iron', 'Cobalt', 'Nickel', 'Copper', 'Zinc', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', 'Bromine', 'Krypton'],
178+
['Sodium', 'Magnesium', '', '', '', '', '', '', '', '', '', '', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon'],
179+
['Potassium', 'Calcium', 'Scandium', 'Titanium', 'Vanadium', 'Chromium', 'Manganese', 'Iron', 'Cobalt', 'Nickel', 'Copper', 'Zinc', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', 'Bromine', 'Krypton'],
146180
['Rubidium', 'Strontium', 'Yttrium', 'Zirconium', 'Niobium', 'Molybdenum', 'Technetium', 'Ruthenium', 'Rhodium', 'Palladium', 'Silver', 'Cadmium', 'Indium', 'Tin', 'Antimony', 'Tellurium', 'Iodine', 'Xenon'],
147-
[' Cesium', ' Barium', '', 'Hafnium', 'Tantalum', 'Tungsten', 'Rhenium', 'Osmium', 'Iridium', 'Platinum', 'Gold', 'Mercury', 'Thallium', 'Lead', 'Bismuth', 'Polonium', 'Astatine', 'Radon'],
148-
[' Francium', ' Radium', '', 'Rutherfordium','Dubnium','Seaborgium','Bohrium','Hassium','Meitnerium','Darmstadtium','Roentgenium','Copernicium','Ununtrium','Ununquadium','Ununpentium','Ununhexium','Ununseptium','Ununoctium'],
181+
['Cesium', 'Barium', '', 'Hafnium', 'Tantalum', 'Tungsten', 'Rhenium', 'Osmium', 'Iridium', 'Platinum', 'Gold', 'Mercury', 'Thallium', 'Lead', 'Bismuth', 'Polonium', 'Astatine', 'Radon'],
182+
['Francium', 'Radium', '', 'Rutherfordium','Dubnium','Seaborgium','Bohrium','Hassium','Meitnerium','Darmstadtium','Roentgenium','Copernicium','Ununtrium','Ununquadium','Ununpentium','Ununhexium','Ununseptium','Ununoctium'],
149183
['', '', 'Lanthanum', 'Cerium', 'Praseodymium', 'Neodymium', 'Promethium', 'Samarium', 'Europium', 'Gadolinium', 'Terbium', 'Dysprosium', 'Holmium', 'Erbium', 'Thulium', 'Ytterbium', 'Lutetium', ''],
150184
['', '', 'Actinium', 'Thorium', 'Protactinium', 'Uranium', 'Neptunium', 'Plutonium', 'Americium', 'Curium', 'Berkelium', 'Californium', 'Einsteinium','Fermium' ,'Mendelevium', 'Nobelium', 'Lawrencium', '' ],
151185
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
@@ -165,7 +199,7 @@ atomic_mass = [[ 1.00794, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0
165199
[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],
166200
[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0]]
167201

168-
z = [[.8, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 1.],
202+
color = [[.8, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 1.],
169203
[.1, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .7, .8, .8, .8, .9, 1.],
170204
[.1, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .6, .7, .8, .8, .9, 1],
171205
[.1, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .7, .8, .8, .9, 1.],
@@ -178,26 +212,46 @@ z = [[.8, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 1.],
178212
[.1, .1, .1, .3, .3, .3, .5, .5, .5, .7, .7, .7, .9, .9, .9, .0, .0, .0],
179213
[.2, .2, .2, .4, .4, .4, .6, .6, .6, .8, .8, .8, 1., 1., 1., .0, .0, .0]]
180214

181-
# Display element name and atomic mass on hover
182-
hover=[]
183-
for x in range(len(symbol)):
184-
hover.append([i + '<br>' + 'Atomic Mass: ' + str(j)
185-
for i, j in zip(element[x], atomic_mass[x])])
186-
187-
# Invert Matrices
188-
symbol = symbol[::-1]
189-
hover = hover[::-1]
190-
z = z[::-1]
191-
192215
# Set Colorscale
193216
colorscale=[[0.0, 'rgb(255,255,255)'], [.2, 'rgb(255, 255, 153)'],
194217
[.4, 'rgb(153, 255, 204)'], [.6, 'rgb(179, 217, 255)'],
195218
[.8, 'rgb(240, 179, 255)'],[1.0, 'rgb(255, 77, 148)']]
196219

220+
# Display element name and atomic mass on hover
221+
hover=[]
222+
for x in range(len(symbol)):
223+
hover.append([i + '<br>' + 'Atomic Mass: ' + str(j) if i else ''
224+
for i, j in zip(element[x], atomic_mass[x])])
225+
226+
import plotly.figure_factory as ff
197227
# Make Annotated Heatmap
198-
fig = ff.create_annotated_heatmap(z, annotation_text=symbol, text=hover,
228+
fig = ff.create_annotated_heatmap(color[::-1], annotation_text=symbol[::-1], text=hover[::-1],
199229
colorscale=colorscale, font_colors=['black'], hoverinfo='text')
200-
fig.update_layout(title_text='Periodic Table')
230+
fig.update_layout(
231+
title_text='Periodic Table',
232+
margin=dict(l=10, r=10, t=10, b=10, pad=10),
233+
xaxis=dict(zeroline=False, showgrid=False),
234+
yaxis=dict(zeroline=False, showgrid=False, scaleanchor="x"),
235+
)
236+
fig.show()
237+
```
238+
239+
Here is the same output using `px.imshow()` with much less array manipulation:
240+
241+
```python
242+
import plotly.express as px
243+
import numpy as np
244+
245+
fig = px.imshow(color, color_continuous_scale=colorscale, aspect="auto",
246+
title='Periodic Table')
247+
fig.update_traces(
248+
text=symbol, texttemplate="%{text}", textfont_size=12,
249+
customdata=np.moveaxis([element, atomic_mass], 0,-1),
250+
hovertemplate="%{customdata[0]}<br>Atomic Mass: %{customdata[1]:.2f}<extra></extra>"
251+
)
252+
fig.update_xaxes(visible=False)
253+
fig.update_yaxes(visible=False)
254+
fig.update_coloraxes(showscale=False)
201255
fig.show()
202256
```
203257

0 commit comments

Comments
 (0)