Skip to content

Commit 49d2c91

Browse files
committed
annotated heatmaps
1 parent b718c8b commit 49d2c91

File tree

1 file changed

+36
-78
lines changed

1 file changed

+36
-78
lines changed

notebooks/annotated_heatmap.md

Lines changed: 36 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ jupyter:
88
format_version: '1.1'
99
jupytext_version: 1.1.1
1010
kernelspec:
11-
display_name: Python 2
11+
display_name: Python 3
1212
language: python
13-
name: python2
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.6.7
1424
plotly:
1525
description: How to make Annotated Heatmaps in Python with Plotly.
1626
display_as: scientific
@@ -24,29 +34,14 @@ jupyter:
2434
permalink: python/annotated_heatmap/
2535
thumbnail: thumbnail/ann_heat.jpg
2636
title: Python Annotated Heatmaps | plotly
37+
v4upgrade: true
2738
---
2839

29-
<!-- #region {"deletable": true, "editable": true} -->
30-
#### New to Plotly?
31-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
32-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
33-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
40+
#### Simple Annotated Heatmap
3441

35-
#### Version Check
36-
Plotly's python package is updated frequently. To upgrade, run `pip install plotly --upgrade`.
37-
<!-- #endregion -->
42+
For more examples with Heatmaps, see [this page](../heatmaps/).
3843

3944
```python
40-
import plotly
41-
plotly.__version__
42-
```
43-
44-
<!-- #region {"deletable": true, "editable": true} -->
45-
#### Simple Annotated Heatmap
46-
<!-- #endregion -->
47-
48-
```python deletable=true editable=true
49-
import plotly.plotly as py
5045
import plotly.figure_factory as ff
5146

5247
z = [[.1, .3, .5, .7, .9],
@@ -56,15 +51,12 @@ z = [[.1, .3, .5, .7, .9],
5651
[.3, .4, .5, .7, 1]]
5752

5853
fig = ff.create_annotated_heatmap(z)
59-
py.iplot(fig, filename='annotated_heatmap')
54+
fig.show()
6055
```
6156

62-
<!-- #region {"deletable": true, "editable": true} -->
6357
#### Defined Colorscale
64-
<!-- #endregion -->
6558

66-
```python deletable=true editable=true
67-
import plotly.plotly as py
59+
```python
6860
import plotly.figure_factory as ff
6961

7062
z = [[.1, .3, .5, .7],
@@ -73,35 +65,29 @@ z = [[.1, .3, .5, .7],
7365
[.9, .7, .5, .3]]
7466

7567
fig = ff.create_annotated_heatmap(z, colorscale='Viridis')
76-
py.iplot(fig, filename='annotated_heatmap_color')
68+
fig.show()
7769
```
7870

79-
<!-- #region {"deletable": true, "editable": true} -->
8071
#### Custom Colorscale
81-
<!-- #endregion -->
8272

83-
```python deletable=true editable=true
84-
import plotly.plotly as py
73+
```python
8574
import plotly.figure_factory as ff
8675

8776
z = [[.1, .3, .5, .7],
8877
[1.0, .8, .6, .4],
8978
[.6, .4, .2, 0.0],
9079
[.9, .7, .5, .3]]
9180

92-
colorscale = [[0, '#66475e'], [1, '#ecbfe0']]
93-
font_colors = ['#efecee', '#3c3636']
81+
colorscale = [[0, 'navy'], [1, 'plum']]
82+
font_colors = ['white', 'black']
9483
fig = ff.create_annotated_heatmap(z, colorscale=colorscale, font_colors=font_colors)
95-
py.iplot(fig, filename='annotated_heatmap_custom_color')
84+
fig.show()
9685
```
9786

98-
<!-- #region {"deletable": true, "editable": true} -->
9987
#### Custom Text and X & Y Labels
10088
set `annotation_text` to a matrix with the same dimmensions as `z`
101-
<!-- #endregion -->
10289

103-
```python deletable=true editable=true
104-
import plotly.plotly as py
90+
```python
10591
import plotly.figure_factory as ff
10692

10793
z = [[.1, .3, .5],
@@ -116,35 +102,31 @@ z_text = [['Win', 'Lose', 'Win'],
116102
['Win', 'Win', 'Lose']]
117103

118104
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
119-
py.iplot(fig, filename='annotated_heatmap_text')
105+
fig.show()
120106
```
121107

122-
<!-- #region {"deletable": true, "editable": true} -->
123108
#### Annotated Heatmap with numpy
124-
<!-- #endregion -->
125109

126-
```python deletable=true editable=true
127-
import plotly.plotly as py
110+
```python
128111
import plotly.figure_factory as ff
129112
import numpy as np
130113

131114
z = np.random.randn(20, 20)
132115
z_text = np.around(z, decimals=2) # Only show rounded value (full value on hover)
133116

134-
fig = ff.create_annotated_heatmap(z, annotation_text=z_text, colorscale='Greys', hoverinfo='z')
117+
fig = ff.create_annotated_heatmap(z, annotation_text=z_text, colorscale='Greys',
118+
hoverinfo='z')
135119

136120
# Make text size smaller
137121
for i in range(len(fig.layout.annotations)):
138122
fig.layout.annotations[i].font.size = 8
139123

140-
py.iplot(fig, filename='annotated_heatmap_numpy')
124+
fig.show()
141125
```
142126

143-
<!-- #region {"deletable": true, "editable": true} -->
144127
#### Custom Hovertext
145-
<!-- #endregion -->
146128

147-
```python deletable=true editable=true
129+
```python
148130
# Add Periodic Table Data
149131
symbol = [['H', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'He'],
150132
['Li', 'Be', '', '', '', '', '', '', '', '', '', '', 'B', 'C', 'N', 'O', 'F', 'Ne'],
@@ -199,9 +181,10 @@ z = [[.8, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 1.],
199181
[.2, .2, .2, .4, .4, .4, .6, .6, .6, .8, .8, .8, 1., 1., 1., .0, .0, .0]]
200182

201183
# Display element name and atomic mass on hover
202-
hover=range(len(symbol))
184+
hover=[]
203185
for x in range(len(symbol)):
204-
hover[x] = [i + '<br>' + 'Atomic Mass: ' + str(j) for i, j in zip(element[x], atomic_mass[x])]
186+
hover.append([i + '<br>' + 'Atomic Mass: ' + str(j)
187+
for i, j in zip(element[x], atomic_mass[x])])
205188

206189
# Invert Matrices
207190
symbol = symbol[::-1]
@@ -214,40 +197,15 @@ colorscale=[[0.0, 'rgb(255,255,255)'], [.2, 'rgb(255, 255, 153)'],
214197
[.8, 'rgb(240, 179, 255)'],[1.0, 'rgb(255, 77, 148)']]
215198

216199
# Make Annotated Heatmap
217-
pt = ff.create_annotated_heatmap(z, annotation_text=symbol, text=hover,
200+
fig = ff.create_annotated_heatmap(z, annotation_text=symbol, text=hover,
218201
colorscale=colorscale, font_colors=['black'], hoverinfo='text')
219-
pt.layout.title = 'Periodic Table'
220-
221-
py.iplot(pt, filename='periodic_table')
202+
fig.update_layout(title_text='Periodic Table')
203+
fig.show()
222204
```
223205

224-
<!-- #region {"deletable": true, "editable": true} -->
225206
#### Reference
226207
For more info on Plotly heatmaps, see: https://plot.ly/python/reference/#heatmap.<br> For more info on using colorscales with Plotly see: https://plot.ly/python/heatmap-and-contour-colorscales/ <br>For more info on annotated_heatmaps, see:
227-
<!-- #endregion -->
228208

229-
```python deletable=true editable=true
209+
```python
230210
help(FF.create_annotated_heatmap)
231211
```
232-
233-
```python deletable=true editable=true
234-
from IPython.display import display, HTML
235-
236-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
237-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
238-
239-
! pip install git+https://github.com/plotly/publisher.git --upgrade
240-
import publisher
241-
publisher.publish(
242-
'annotated_heatmap.ipynb', 'python/annotated_heatmap/', 'Python Annotated Heatmaps | plotly',
243-
'How to make Annotated Heatmaps in Python with Plotly.',
244-
title = 'Python Annotated Heatmaps | plotly',
245-
name = 'Annotated Heatmaps',
246-
thumbnail='thumbnail/ann_heat.jpg', language='python',
247-
has_thumbnail='true', display_as='scientific', order=4,
248-
ipynb= '~notebook_demo/35')
249-
```
250-
251-
```python deletable=true editable=true
252-
253-
```

0 commit comments

Comments
 (0)