Skip to content

Commit 3214f9c

Browse files
documenting text_auto and deprecating FFs
1 parent 240155b commit 3214f9c

File tree

11 files changed

+163
-160
lines changed

11 files changed

+163
-160
lines changed

doc/python/2D-Histogram.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jupyter:
88
format_version: '1.3'
99
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3 (ipykernel)
11+
display_name: Python 3
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.11
23+
version: 3.8.11
2424
plotly:
2525
description: How to make 2D Histograms in Python with Plotly.
2626
display_as: statistical
@@ -82,14 +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}")`
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.
8690

8791
```python
8892
import plotly.express as px
8993
df = px.data.tips()
9094

91-
fig = px.density_heatmap(df, x="total_bill", y="tip")
92-
fig.update_traces(texttemplate="%{z}")
95+
fig = px.density_heatmap(df, x="total_bill", y="tip", text_auto=True)
9396
fig.show()
9497
```
9598

doc/python/annotated-heatmap.md

+43-127
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jupyter:
88
format_version: '1.3'
99
jupytext_version: 1.13.4
1010
kernelspec:
11-
display_name: Python 3 (ipykernel)
11+
display_name: Python 3
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.11
23+
version: 3.8.11
2424
plotly:
2525
description: How to make Annotated Heatmaps in Python with Plotly.
2626
display_as: scientific
@@ -34,9 +34,11 @@ jupyter:
3434
thumbnail: thumbnail/ann_heat.jpg
3535
---
3636

37-
### Annotated Heatmaps with plotly.express and px.imshow
37+
### Annotated Heatmaps with Plotly Express
3838

39-
These examples use [px.imshow](/python/imshow) to create Annotated Heatmaps. px.imshow is the recommended way to create heatmaps with z-annotations.
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.
4042

4143

4244
#### Basic Annotated Heatmap for z-annotations
@@ -46,33 +48,19 @@ After creating a figure with `px.imshow`, you can add z-annotations with `.updat
4648
```python
4749
import plotly.express as px
4850

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
58-
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})
51+
z = [[.1, .3, .5, .7, .9],
52+
[1, .8, .6, .4, .2],
53+
[.2, 0, .5, .7, .9],
54+
[.9, .8, .4, .2, 0],
55+
[.3, .4, .5, .7, 1]]
6956

57+
fig = px.imshow(z, text_auto=True)
7058
fig.show()
7159
```
7260

73-
### Annotated Heatmaps with [figure factory](/python/figure-factories/). For more examples with Heatmaps, see [this page](/python/heatmaps/).
61+
### Deprecated Figure Factory
7462

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/).
63+
The remaining examples show how to create Annotated Heatmaps with the deprecated `create_annotated_heatmap` [figure factory](/python/figure-factories/).
7664

7765

7866
#### Simple Annotated Heatmap
@@ -90,54 +78,48 @@ fig = ff.create_annotated_heatmap(z)
9078
fig.show()
9179
```
9280

93-
#### Defined Colorscale
81+
#### Custom Text and X & Y Labels
82+
set `annotation_text` to a matrix with the same dimensions as `z`
83+
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**
9485
9586
```python
9687
import plotly.figure_factory as ff
9788

98-
z = [[.1, .3, .5, .7],
99-
[1, .8, .6, .4],
100-
[.6, .4, .2, .0],
101-
[.9, .7, .5, .3]]
102-
103-
fig = ff.create_annotated_heatmap(z, colorscale='Viridis')
104-
fig.show()
105-
```
106-
107-
#### Custom Colorscale
89+
z = [[.1, .3, .5],
90+
[1.0, .8, .6],
91+
[.6, .4, .2]]
10892

109-
```python
110-
import plotly.figure_factory as ff
93+
x = ['Team A', 'Team B', 'Team C']
94+
y = ['Game Three', 'Game Two', 'Game One']
11195

112-
z = [[.1, .3, .5, .7],
113-
[1.0, .8, .6, .4],
114-
[.6, .4, .2, 0.0],
115-
[.9, .7, .5, .3]]
96+
z_text = [['Win', 'Lose', 'Win'],
97+
['Lose', 'Lose', 'Win'],
98+
['Win', 'Win', 'Lose']]
11699

117-
colorscale = [[0, 'navy'], [1, 'plum']]
118-
font_colors = ['white', 'black']
119-
fig = ff.create_annotated_heatmap(z, colorscale=colorscale, font_colors=font_colors)
100+
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
120101
fig.show()
121102
```
122103

123-
#### Custom Text and X & Y Labels
124-
set `annotation_text` to a matrix with the same dimensions as `z`
104+
Here is the same figure using `px.imshow()`
125105

126106
```python
127-
import plotly.figure_factory as ff
107+
import plotly.express as px
108+
109+
x = ['Team A', 'Team B', 'Team C']
110+
y = ['Game One', 'Game Two', 'Game Three']
128111

129112
z = [[.1, .3, .5],
130113
[1.0, .8, .6],
131114
[.6, .4, .2]]
132115

133-
x = ['Team A', 'Team B', 'Team C']
134-
y = ['Game Three', 'Game Two', 'Game One']
135-
136116
z_text = [['Win', 'Lose', 'Win'],
137117
['Lose', 'Lose', 'Win'],
138118
['Win', 'Win', 'Lose']]
139119

140-
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
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")
141123
fig.show()
142124
```
143125

@@ -161,82 +143,16 @@ for i in range(len(fig.layout.annotations)):
161143
fig.show()
162144
```
163145

164-
#### Custom Hovertext
146+
Here is the same figure using `px.imshow()`
165147

166148
```python
167-
# Add Periodic Table Data
168-
symbol = [['H', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'He'],
169-
['Li', 'Be', '', '', '', '', '', '', '', '', '', '', 'B', 'C', 'N', 'O', 'F', 'Ne'],
170-
['Na', 'Mg', '', '', '', '', '', '', '', '', '', '', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar'],
171-
['K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr'],
172-
['Rb ', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe' ],
173-
['Cs', 'Ba', '', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn' ],
174-
['Fr', 'Ra', '', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Uut', 'Fl', 'Uup', 'Lv', 'Uus', 'Uuo'],
175-
['', '', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', ''],
176-
['', '', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', '' ],
177-
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
178-
['', 'Alkali Metal', '', '', 'Transition Metal', '', '', 'Actinide', '', '', 'Semimetal', '', '', 'Halogen', '', '', '', ''],
179-
['', 'Alkaline Metal', '', '', 'Lanthanide', '', '', 'Basic Metal', '', '', 'Nonmetal', '', '', 'Noble Gas', '', '', '', '']]
180-
181-
element = [['Hydrogen', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Helium'],
182-
['Lithium', 'Beryllium', '', '', '', '', '', '', '', '', '', '', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon'],
183-
['Sodium', 'Magnesium', '', '', '', '', '', '', '', '', '', '', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', ' Argon'],
184-
['Potassium', ' Calcium', ' Scandium', ' Titanium', ' Vanadium', ' Chromium', 'Manganese', 'Iron', 'Cobalt', 'Nickel', 'Copper', 'Zinc', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', 'Bromine', 'Krypton'],
185-
['Rubidium', 'Strontium', 'Yttrium', 'Zirconium', 'Niobium', 'Molybdenum', 'Technetium', 'Ruthenium', 'Rhodium', 'Palladium', 'Silver', 'Cadmium', 'Indium', 'Tin', 'Antimony', 'Tellurium', 'Iodine', 'Xenon'],
186-
[' Cesium', ' Barium', '', 'Hafnium', 'Tantalum', 'Tungsten', 'Rhenium', 'Osmium', 'Iridium', 'Platinum', 'Gold', 'Mercury', 'Thallium', 'Lead', 'Bismuth', 'Polonium', 'Astatine', 'Radon'],
187-
[' Francium', ' Radium', '', 'Rutherfordium','Dubnium','Seaborgium','Bohrium','Hassium','Meitnerium','Darmstadtium','Roentgenium','Copernicium','Ununtrium','Ununquadium','Ununpentium','Ununhexium','Ununseptium','Ununoctium'],
188-
['', '', 'Lanthanum', 'Cerium', 'Praseodymium', 'Neodymium', 'Promethium', 'Samarium', 'Europium', 'Gadolinium', 'Terbium', 'Dysprosium', 'Holmium', 'Erbium', 'Thulium', 'Ytterbium', 'Lutetium', ''],
189-
['', '', 'Actinium', 'Thorium', 'Protactinium', 'Uranium', 'Neptunium', 'Plutonium', 'Americium', 'Curium', 'Berkelium', 'Californium', 'Einsteinium','Fermium' ,'Mendelevium', 'Nobelium', 'Lawrencium', '' ],
190-
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
191-
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
192-
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']]
193-
194-
atomic_mass = [[ 1.00794, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 4.002602],
195-
[ 6.941, 9.012182, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 10.811, 12.0107, 14.0067, 15.9994, 18.9984032, 20.1797],
196-
[ 22.98976928, 24.3050, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 26.9815386, 28.0855, 30.973762, 32.065, 35.453, 39.948],
197-
[ 39.0983, 40.078, 44.955912, 47.867, 50.9415, 51.9961, 54.938045, 55.845, 58.933195, 58.6934, 63.546, 65.38, 69.723, 72.64, 74.92160, 78.96, 79.904, 83.798],
198-
[ 85.4678, 87.62, 88.90585, 91.224, 92.90638, 95.96, 98, 101.07, 102.90550, 106.42, 107.8682, 112.411, 114.818, 118.710, 121.760, 127.60, 126.90447, 131.293],
199-
[ 132.9054519, 137.327, .0, 178.49, 180.94788, 183.84, 186.207, 190.23, 192.217, 195.084, 196.966569, 200.59, 204.3833, 207.2, 208.98040, 209, 210, 222],
200-
[223, 226, .0, 267, 268, 271, 272, 270, 276, 281, 280, 285, 284, 289, 288, 293, 'unknown', 294],
201-
[.0, .0, 138.90547, 140.116, 140.90765, 144.242, 145, 150.36, 151.964, 157.25, 158.92535, 162.500, 164.93032, 167.259, 168.93421, 173.054, 174.9668, .0],
202-
[.0, .0, 227, 232.03806, 231.03588, 238.02891, 237, 244, 243, 247, 247, 251, 252, 257, 258, 259, 262, .0],
203-
[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],
204-
[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],
205-
[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0]]
206-
207-
z = [[.8, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 1.],
208-
[.1, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .7, .8, .8, .8, .9, 1.],
209-
[.1, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .6, .7, .8, .8, .9, 1],
210-
[.1, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .7, .8, .8, .9, 1.],
211-
[.1, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .6, .7, .7, .9, 1.],
212-
[.1, .2, .4, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .6, .6, .7, .9, 1.],
213-
[.1, .2, .5, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .6, .6, .6, .9, 1.],
214-
[.0, .0, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .0],
215-
[.0, .0, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .0],
216-
[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],
217-
[.1, .1, .1, .3, .3, .3, .5, .5, .5, .7, .7, .7, .9, .9, .9, .0, .0, .0],
218-
[.2, .2, .2, .4, .4, .4, .6, .6, .6, .8, .8, .8, 1., 1., 1., .0, .0, .0]]
219-
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)
224-
for i, j in zip(element[x], atomic_mass[x])])
225-
226-
# Invert Matrices
227-
symbol = symbol[::-1]
228-
hover = hover[::-1]
229-
z = z[::-1]
230-
231-
# Set Colorscale
232-
colorscale=[[0.0, 'rgb(255,255,255)'], [.2, 'rgb(255, 255, 153)'],
233-
[.4, 'rgb(153, 255, 204)'], [.6, 'rgb(179, 217, 255)'],
234-
[.8, 'rgb(240, 179, 255)'],[1.0, 'rgb(255, 77, 148)']]
235-
236-
# Make Annotated Heatmap
237-
fig = ff.create_annotated_heatmap(z, annotation_text=symbol, text=hover,
238-
colorscale=colorscale, font_colors=['black'], hoverinfo='text')
239-
fig.update_layout(title_text='Periodic Table')
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")
240156
fig.show()
241157
```
242158

doc/python/bar-charts.md

+18-4
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.4.2
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.7.7
23+
version: 3.8.11
2424
plotly:
2525
description: How to make Bar Charts in Python with Plotly.
2626
display_as: basic
@@ -98,7 +98,7 @@ snippet_url = 'https://dash-gallery.plotly.host/python-docs-dash-snippets/'
9898
IFrame(snippet_url + 'bar-charts', width='100%', height=630)
9999
```
100100

101-
### Customize bar charts with Plotly Express
101+
### Customized bar charts with Plotly Express
102102

103103
The bar plot can be customized using keyword arguments, for example to use [continuous color](https://plotly.com/python/colorscales/), as below, or [discrete color](/python/discrete-color/), as above.
104104

@@ -147,6 +147,20 @@ fig = px.bar(df, x="medal", y="count", color="nation",
147147
fig.show()
148148
```
149149

150+
#### Bar Charts with Text
151+
152+
*New in v5.5*
153+
154+
You can add text to bars 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.
155+
156+
```python
157+
import plotly.express as px
158+
df = px.data.medals_long()
159+
160+
fig = px.bar(df, x="medal", y="count", color="nation", text_auto=True)
161+
fig.show()
162+
```
163+
150164
#### Facetted subplots
151165

152166
Use the keyword arguments `facet_row` (resp. `facet_col`) to create facetted subplots, where different rows (resp. columns) correspond to different values of the dataframe column specified in `facet_row`.

doc/python/county-choropleth.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ This page describes a [legacy "figure factory" method](/python/figure-factories/
4141

4242

4343
#### Required Packages
44-
`geopandas`, `pyshp` and `shapely` must be installed for this figure factory.
44+
`plotly_geo`, `pyshp` and `shapely` must be installed for this figure factory.
4545

4646
Run the following commands to install the correct versions of the following modules:
4747

4848
```python
49+
!pip install plotly-geo
4950
!pip install geopandas==0.3.0
5051
!pip install pyshp==1.2.10
5152
!pip install shapely==1.6.3

doc/python/figure-factories.md

+8-4
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: Figure Factories are dedicated functions for creating very specific
2626
types of plots.
@@ -39,7 +39,6 @@ The `plotly.figure_factory` module contains dedicated functions for creating ver
3939

4040
The following types of plots are still difficult to create with Graph Objects or Plotly Express and therefore the corresponding Figure Factories are *not* deprecated:
4141

42-
* [Annotated Heatmaps](/python/annotated-heatmap/)
4342
* [Dendrograms](/python/dendrogram/)
4443
* [Hexagonal Binning Tile Map](/python/hexbin-mapbox/)
4544
* [Quiver Plots](/python/quiver-plots/)
@@ -50,10 +49,15 @@ The following types of plots are still difficult to create with Graph Objects or
5049

5150
Deprecated "legacy" Figure Factories include:
5251

52+
* [Annotated Heatmaps](/python/annotated-heatmap/), deprecated by [heatmaps with `px.imshow()`](/python/heatmaps/)
5353
* [County Choropleth Maps](/python/county-choropleth/), deprecated by regular [Choropleth maps with GeoJSON input](/python/choropleth-maps/)
5454
* [Distplots](/python/distplot/), mostly deprecated by [`px.histogram`](/python/histograms/) except for KDE plots, which `px.histogram` doesn't support yet
5555
* [Gantt Charts](/python/gantt/), deprecated by [`px.timeline`](/python/gantt/)
5656

5757
#### Reference
5858

5959
For more information about the contents of `plotly.figure_factory`, including deprecated methods, please refer to our [API Reference documentation](https://plotly.com/python-api-reference/plotly.figure_factory.html).
60+
61+
```python
62+
63+
```

0 commit comments

Comments
 (0)