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