1
1
---
2
2
jupyter :
3
3
jupytext :
4
+ notebook_metadata_filter :all
4
5
text_representation :
5
6
extension :.md
6
7
format_name :markdown
@@ -10,6 +11,16 @@ jupyter:
10
11
display_name :Python 3
11
12
language :python
12
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
13
24
plotly :
14
25
description :How to make Heatmaps in Python with Plotly.
15
26
display_as :scientific
@@ -24,145 +35,114 @@ jupyter:
24
35
redirect_from :python/heatmap/
25
36
thumbnail :thumbnail/heatmap.jpg
26
37
title :Python Heatmaps | plotly
38
+ v4upgrade :true
27
39
---
28
40
29
- ####New to Plotly?
30
- 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/ ) .
31
- <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 ) .
32
- <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!
33
- ####Version Check
34
- Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.
35
-
36
- ``` python
37
- import plotly
38
- plotly.__version__
39
- ```
40
-
41
41
###Basic Heatmap
42
42
43
43
``` python
44
- import plotly.plotlyas py
45
- import plotly.graph_objsas go
44
+ import plotly.graph_objectsas go
46
45
47
- trace= go.Heatmap(z = [[1 ,20 ,30 ],
46
+ fig= go.Figure(data = go.Heatmap(
47
+ z = [[1 ,20 ,30 ],
48
48
[20 ,1 ,60 ],
49
- [30 ,60 ,1 ]])
50
- data= [trace]
51
- py.iplot(data,filename = ' basic-heatmap' )
49
+ [30 ,60 ,1 ]]))
50
+ fig.show()
52
51
```
53
52
54
53
###Heatmap with Categorical Axis Labels
55
54
56
55
``` python
57
- import plotly.plotlyas py
58
- import plotly.graph_objsas go
56
+ import plotly.graph_objectsas go
59
57
60
- trace= go.Heatmap(z = [[1 ,20 ,30 ,50 ,1 ], [20 ,1 ,60 ,80 ,30 ], [30 ,60 ,1 ,- 10 ,20 ]],
58
+ fig= go.Figure(data = go.Heatmap(
59
+ z = [[1 ,20 ,30 ,50 ,1 ], [20 ,1 ,60 ,80 ,30 ], [30 ,60 ,1 ,- 10 ,20 ]],
61
60
x = [' Monday' ,' Tuesday' ,' Wednesday' ,' Thursday' ,' Friday' ],
62
- y = [' Morning' ,' Afternoon' ,' Evening' ])
63
- data= [trace]
64
- py.iplot(data,filename = ' labelled-heatmap' )
61
+ y = [' Morning' ,' Afternoon' ,' Evening' ]))
62
+ fig.show()
65
63
```
66
64
67
65
###Heatmap with Unequal Block Sizes
68
66
69
67
70
68
``` python
69
+ import plotly.graph_objectsas go
71
70
import numpyas np
72
- import plotly.plotlyas py
73
-
74
- def spiral (th ):
75
- a= 1.120529
76
- b= 0.306349
77
- r= a* np.exp(- b* th)
78
- return (r* np.cos(th), r* np.sin(th))
79
-
80
- nspiral= 2 # number of spiral loops
81
-
82
- th= np.linspace(- np.pi/ 13 ,2 * np.pi* nspiral,1000 );# angle
83
- (x,y)= spiral(th)
84
-
85
- # shift the spiral north so that it is centered
86
- yshift= (1.6 - (max (y)- min (y)))/ 2
87
-
88
- s= dict (x = - x+ x[0 ],y = y- y[0 ]+ yshift,
89
- line = dict (color = ' white' ,width = 3 ))
90
71
91
72
# Build the rectangles as a heatmap
92
73
# specify the edges of the heatmap squares
93
- phi= (1 + np.sqrt(5 ) )/ 2 .
74
+ phi= (1 + np.sqrt(5 ) )/ 2 .# golden ratio
94
75
xe= [0 ,1 ,1 + (1 / (phi** 4 )),1 + (1 / (phi** 3 )), phi]
95
- ye= [0 ,1 / (phi** 3 ),1 / phi** 3 + 1 / phi** 4 ,1 / (phi** 2 ),1 ]
76
+ ye= [0 ,1 / (phi** 3 ),1 / phi** 3 + 1 / phi** 4 ,1 / (phi** 2 ),1 ]
96
77
97
78
z= [ [13 ,3 ,3 ,5 ],
98
79
[13 ,2 ,1 ,5 ],
99
80
[13 ,10 ,11 ,12 ],
100
81
[13 ,8 ,8 ,8 ]
101
82
]
102
83
103
- hm= dict (x = np.sort(xe),
84
+ fig= go.Figure(data = go.Heatmap(
85
+ x = np.sort(xe),
104
86
y = np.sort(ye)+ yshift,
105
87
z = z,
106
88
type = ' heatmap' ,
107
- colorscale = ' Viridis' )
89
+ colorscale = ' Viridis' ))
90
+
91
+ # Add spiral line plot
92
+
93
+ def spiral (th ):
94
+ a= 1.120529
95
+ b= 0.306349
96
+ r= a* np.exp(- b* th)
97
+ return (r* np.cos(th), r* np.sin(th))
98
+
99
+ theta= np.linspace(- np.pi/ 13 ,4 * np.pi,1000 );# angle
100
+ (x,y)= spiral(theta)
101
+
102
+ # shift the spiral north so that it is centered
103
+ yshift= (1.6 - (max (y)- min (y)))/ 2
104
+ fig.add_trace(go.Scatter(x = - x+ x[0 ],y = y- y[0 ]+ yshift,
105
+ line = dict (color = ' white' ,width = 3 )))
108
106
109
107
axis_template= dict (range = [0 ,1.6 ],autorange = False ,
110
108
showgrid = False ,zeroline = False ,
111
109
linecolor = ' black' ,showticklabels = False ,
112
110
ticks = ' ' )
113
111
114
- layout = dict ( margin = dict (t = 200 ,r = 200 ,b = 200 ,l = 200 ),
112
+ fig.update_layout( margin = dict (t = 200 ,r = 200 ,b = 200 ,l = 200 ),
115
113
xaxis = axis_template,
116
114
yaxis = axis_template,
117
115
showlegend = False ,
118
116
width = 700 ,height = 700 ,
119
117
autosize = False )
120
118
121
- figure= dict (data = [s, hm],layout = layout)
122
-
123
- py.iplot(figure,filename = ' golden spiral' ,height = 750 )
124
-
119
+ fig.show()
125
120
```
126
121
127
122
###Heatmap with Datetime Axis
128
123
129
124
``` python
130
-
125
+ import plotly.graph_objects as go
131
126
import datetime
132
127
import numpyas np
133
- import plotly.plotlyas py
134
- import plotly.graph_objsas go
135
128
136
129
programmers= [' Alex' ,' Nicole' ,' Sara' ,' Etienne' ,' Chelsea' ,' Jody' ,' Marianne' ]
137
130
138
131
base= datetime.datetime.today()
139
- date_list= [base- datetime.timedelta(days = x)for xin range (0 ,180 )]
140
-
141
- z= []
142
-
143
- for prgmrin programmers:
144
- new_row= []
145
- for datein date_list:
146
- new_row.append( np.random.poisson() )
147
- z.append(list (new_row))
148
-
149
- data= [
150
- go.Heatmap(
132
+ dates= base- np.arange(180 )* datetime.timedelta(days = 1 )
133
+ z= np.random.poisson(size = (len (programmers),len (dates)))
134
+
135
+ fig= go.Figure(data = go.Heatmap(
151
136
z = z,
152
- x = date_list ,
137
+ x = dates ,
153
138
y = programmers,
154
- colorscale = ' Viridis' ,
155
- )
156
- ]
139
+ colorscale = ' Viridis' ))
157
140
158
- layout = go.Layout (
141
+ fig.update_layout (
159
142
title = ' GitHub commits per day' ,
160
- xaxis = dict (ticks = ' ' ,nticks = 36 ),
161
- yaxis = dict (ticks = ' ' )
162
- )
143
+ xaxis_nticks = 36 )
163
144
164
- fig= go.Figure(data = data,layout = layout)
165
- py.iplot(fig,filename = ' datetime-heatmap' )
145
+ fig.show()
166
146
```
167
147
168
148
###Dash Example
@@ -182,28 +162,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-heatmapplot/code", width=
182
162
183
163
####Reference
184
164
Seehttps://plot.ly/python/reference/#heatmap for more information and chart attribute options!
185
-
186
-
187
- ``` python
188
- from IPython.displayimport display,HTML
189
-
190
- display(HTML(' <link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700rel="stylesheet" type="text/css" />' ))
191
- display(HTML(' <link rel="stylesheet" type="text/csshref="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">' ))
192
-
193
- ! pip install git+ https:// github.com/ plotly/ publisher.git-- upgrade
194
-
195
- import publisher
196
- publisher.publish(
197
- ' heatmaps.ipynb' ,' python/heatmaps/' ,' Heatmaps | plotly' ,
198
- ' How to make Heatmaps in Python with Plotly.' ,
199
- title = ' Python Heatmaps | plotly' ,
200
- name = ' Heatmaps' ,
201
- has_thumbnail = ' true' ,thumbnail = ' thumbnail/heatmap.jpg' ,
202
- language = ' python' ,page_type = ' example_index' ,
203
- display_as = ' scientific' ,order = 3 ,
204
- ipynb = ' ~notebook_demo/33' ,redirect_from = ' python/heatmap/' )
205
- ```
206
-
207
- ``` python
208
-
209
- ```