Skip to content

Commit 8eda39c

Browse files
committed
first pass at heatmap nb
1 parent 3c030ce commit 8eda39c

File tree

1 file changed

+56
-101
lines changed

1 file changed

+56
-101
lines changed

notebooks/heatmaps.md

Lines changed: 56 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
@@ -10,6 +11,16 @@ jupyter:
1011
display_name: Python 3
1112
language: python
1213
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
1324
plotly:
1425
description: How to make Heatmaps in Python with Plotly.
1526
display_as: scientific
@@ -24,145 +35,114 @@ jupyter:
2435
redirect_from: python/heatmap/
2536
thumbnail: thumbnail/heatmap.jpg
2637
title: Python Heatmaps | plotly
38+
v4upgrade: true
2739
---
2840

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-
4141
### Basic Heatmap
4242

4343
```python
44-
import plotly.plotly as py
45-
import plotly.graph_objs as go
44+
import plotly.graph_objects as go
4645

47-
trace = go.Heatmap(z=[[1, 20, 30],
46+
fig = go.Figure(data=go.Heatmap(
47+
z=[[1, 20, 30],
4848
[20, 1, 60],
49-
[30, 60, 1]])
50-
data=[trace]
51-
py.iplot(data, filename='basic-heatmap')
49+
[30, 60, 1]]))
50+
fig.show()
5251
```
5352

5453
### Heatmap with Categorical Axis Labels
5554

5655
```python
57-
import plotly.plotly as py
58-
import plotly.graph_objs as go
56+
import plotly.graph_objects as go
5957

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]],
6160
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()
6563
```
6664

6765
### Heatmap with Unequal Block Sizes
6866

6967

7068
```python
69+
import plotly.graph_objects as go
7170
import numpy as np
72-
import plotly.plotly as 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))
9071

9172
# Build the rectangles as a heatmap
9273
# specify the edges of the heatmap squares
93-
phi = ( 1+np.sqrt(5) )/2.
74+
phi = (1 + np.sqrt(5) )/2. # golden ratio
9475
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]
9677

9778
z = [ [13,3,3,5],
9879
[13,2,1,5],
9980
[13,10,11,12],
10081
[13,8,8,8]
10182
]
10283

103-
hm = dict(x = np.sort(xe),
84+
fig = go.Figure(data=go.Heatmap(
85+
x = np.sort(xe),
10486
y = np.sort(ye)+yshift,
10587
z = z,
10688
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)))
108106

109107
axis_template = dict(range = [0,1.6], autorange = False,
110108
showgrid = False, zeroline = False,
111109
linecolor = 'black', showticklabels = False,
112110
ticks = '' )
113111

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),
115113
xaxis = axis_template,
116114
yaxis = axis_template,
117115
showlegend = False,
118116
width = 700, height = 700,
119117
autosize = False )
120118

121-
figure = dict(data=[s, hm],layout=layout)
122-
123-
py.iplot(figure, filename='golden spiral', height=750)
124-
119+
fig.show()
125120
```
126121

127122
### Heatmap with Datetime Axis
128123

129124
```python
130-
125+
import plotly.graph_objects as go
131126
import datetime
132127
import numpy as np
133-
import plotly.plotly as py
134-
import plotly.graph_objs as go
135128

136129
programmers = ['Alex','Nicole','Sara','Etienne','Chelsea','Jody','Marianne']
137130

138131
base = datetime.datetime.today()
139-
date_list = [base - datetime.timedelta(days=x) for x in range(0, 180)]
140-
141-
z = []
142-
143-
for prgmr in programmers:
144-
new_row = []
145-
for date in 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(
151136
z=z,
152-
x=date_list,
137+
x=dates,
153138
y=programmers,
154-
colorscale='Viridis',
155-
)
156-
]
139+
colorscale='Viridis'))
157140

158-
layout = go.Layout(
141+
fig.update_layout(
159142
title='GitHub commits per day',
160-
xaxis = dict(ticks='', nticks=36),
161-
yaxis = dict(ticks='' )
162-
)
143+
xaxis_nticks=36)
163144

164-
fig = go.Figure(data=data, layout=layout)
165-
py.iplot(fig, filename='datetime-heatmap')
145+
fig.show()
166146
```
167147

168148
### Dash Example
@@ -182,28 +162,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-heatmapplot/code", width=
182162

183163
#### Reference
184164
See https://plot.ly/python/reference/#heatmap for more information and chart attribute options!
185-
186-
187-
```python
188-
from IPython.display import 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-
```

0 commit comments

Comments
 (0)