@@ -8,9 +8,19 @@ jupyter:
8
8
format_version : ' 1.1'
9
9
jupytext_version : 1.1.1
10
10
kernelspec :
11
- display_name : Python 2
11
+ display_name : Python 3
12
12
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
14
24
plotly :
15
25
description : How to make Annotated Heatmaps in Python with Plotly.
16
26
display_as : scientific
@@ -24,29 +34,14 @@ jupyter:
24
34
permalink : python/annotated_heatmap/
25
35
thumbnail : thumbnail/ann_heat.jpg
26
36
title : Python Annotated Heatmaps | plotly
37
+ v4upgrade : true
27
38
---
28
39
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
34
41
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/ ) .
38
43
39
44
``` 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
50
45
import plotly.figure_factory as ff
51
46
52
47
z = [[.1 , .3 , .5 , .7 , .9 ],
@@ -56,15 +51,12 @@ z = [[.1, .3, .5, .7, .9],
56
51
[.3 , .4 , .5 , .7 , 1 ]]
57
52
58
53
fig = ff.create_annotated_heatmap(z)
59
- py.iplot(fig, filename = ' annotated_heatmap ' )
54
+ fig.show( )
60
55
```
61
56
62
- <!-- #region {"deletable": true, "editable": true} -->
63
57
#### Defined Colorscale
64
- <!-- #endregion -->
65
58
66
- ``` python deletable=true editable=true
67
- import plotly.plotly as py
59
+ ``` python
68
60
import plotly.figure_factory as ff
69
61
70
62
z = [[.1 , .3 , .5 , .7 ],
@@ -73,35 +65,29 @@ z = [[.1, .3, .5, .7],
73
65
[.9 , .7 , .5 , .3 ]]
74
66
75
67
fig = ff.create_annotated_heatmap(z, colorscale = ' Viridis' )
76
- py.iplot(fig, filename = ' annotated_heatmap_color ' )
68
+ fig.show( )
77
69
```
78
70
79
- <!-- #region {"deletable": true, "editable": true} -->
80
71
#### Custom Colorscale
81
- <!-- #endregion -->
82
72
83
- ``` python deletable=true editable=true
84
- import plotly.plotly as py
73
+ ``` python
85
74
import plotly.figure_factory as ff
86
75
87
76
z = [[.1 , .3 , .5 , .7 ],
88
77
[1.0 , .8 , .6 , .4 ],
89
78
[.6 , .4 , .2 , 0.0 ],
90
79
[.9 , .7 , .5 , .3 ]]
91
80
92
- colorscale = [[0 , ' #66475e ' ], [1 , ' #ecbfe0 ' ]]
93
- font_colors = [' #efecee ' , ' #3c3636 ' ]
81
+ colorscale = [[0 , ' navy ' ], [1 , ' plum ' ]]
82
+ font_colors = [' white ' , ' black ' ]
94
83
fig = ff.create_annotated_heatmap(z, colorscale = colorscale, font_colors = font_colors)
95
- py.iplot(fig, filename = ' annotated_heatmap_custom_color ' )
84
+ fig.show( )
96
85
```
97
86
98
- <!-- #region {"deletable": true, "editable": true} -->
99
87
#### Custom Text and X & Y Labels
100
88
set ` annotation_text ` to a matrix with the same dimmensions as ` z `
101
- <!-- #endregion -->
102
89
103
- ``` python deletable=true editable=true
104
- import plotly.plotly as py
90
+ ``` python
105
91
import plotly.figure_factory as ff
106
92
107
93
z = [[.1 , .3 , .5 ],
@@ -116,35 +102,31 @@ z_text = [['Win', 'Lose', 'Win'],
116
102
[' Win' , ' Win' , ' Lose' ]]
117
103
118
104
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( )
120
106
```
121
107
122
- <!-- #region {"deletable": true, "editable": true} -->
123
108
#### Annotated Heatmap with numpy
124
- <!-- #endregion -->
125
109
126
- ``` python deletable=true editable=true
127
- import plotly.plotly as py
110
+ ``` python
128
111
import plotly.figure_factory as ff
129
112
import numpy as np
130
113
131
114
z = np.random.randn(20 , 20 )
132
115
z_text = np.around(z, decimals = 2 ) # Only show rounded value (full value on hover)
133
116
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' )
135
119
136
120
# Make text size smaller
137
121
for i in range (len (fig.layout.annotations)):
138
122
fig.layout.annotations[i].font.size = 8
139
123
140
- py.iplot(fig, filename = ' annotated_heatmap_numpy ' )
124
+ fig.show( )
141
125
```
142
126
143
- <!-- #region {"deletable": true, "editable": true} -->
144
127
#### Custom Hovertext
145
- <!-- #endregion -->
146
128
147
- ``` python deletable=true editable=true
129
+ ``` python
148
130
# Add Periodic Table Data
149
131
symbol = [[' H' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' He' ],
150
132
[' 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.],
199
181
[.2 , .2 , .2 , .4 , .4 , .4 , .6 , .6 , .6 , .8 , .8 , .8 , 1 ., 1 ., 1 ., .0 , .0 , .0 ]]
200
182
201
183
# Display element name and atomic mass on hover
202
- hover= range ( len (symbol))
184
+ hover= []
203
185
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])])
205
188
206
189
# Invert Matrices
207
190
symbol = symbol[::- 1 ]
@@ -214,40 +197,15 @@ colorscale=[[0.0, 'rgb(255,255,255)'], [.2, 'rgb(255, 255, 153)'],
214
197
[.8 , ' rgb(240, 179, 255)' ],[1.0 , ' rgb(255, 77, 148)' ]]
215
198
216
199
# 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,
218
201
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()
222
204
```
223
205
224
- <!-- #region {"deletable": true, "editable": true} -->
225
206
#### Reference
226
207
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 -->
228
208
229
- ``` python deletable=true editable=true
209
+ ``` python
230
210
help (FF .create_annotated_heatmap)
231
211
```
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