Skip to content

Commit 3ea8dc5

Browse files
authored
Merge pull request #25 from plotly/heatmap
heatmap notebook
2 parents 0a47d7e + 6e48ed2 commit 3ea8dc5

File tree

1 file changed

+54
-102
lines changed

1 file changed

+54
-102
lines changed

notebooks/heatmaps.md

Lines changed: 54 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ jupyter:
1111
display_name: Python 3
1212
language: python
1313
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 Heatmaps in Python with Plotly.
1626
display_as: scientific
@@ -25,145 +35,112 @@ jupyter:
2535
redirect_from: python/heatmap/
2636
thumbnail: thumbnail/heatmap.jpg
2737
title: Python Heatmaps | plotly
38+
v4upgrade: true
2839
---
2940

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-
4241
### Basic Heatmap
4342

4443
```python
45-
import plotly.plotly as py
46-
import plotly.graph_objs as go
44+
import plotly.graph_objects as go
4745

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

5553
### Heatmap with Categorical Axis Labels
5654

5755
```python
58-
import plotly.plotly as py
59-
import plotly.graph_objs as go
56+
import plotly.graph_objects as go
6057

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]],
6260
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()
6663
```
6764

6865
### Heatmap with Unequal Block Sizes
6966

7067

7168
```python
69+
import plotly.graph_objects as go
7270
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))
9171

9272
# Build the rectangles as a heatmap
9373
# specify the edges of the heatmap squares
94-
phi = ( 1+np.sqrt(5) )/2.
74+
phi = (1 + np.sqrt(5) )/2. # golden ratio
9575
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]
9777

9878
z = [ [13,3,3,5],
9979
[13,2,1,5],
10080
[13,10,11,12],
10181
[13,8,8,8]
10282
]
10383

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),
10687
z = z,
10788
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)))
109104

110105
axis_template = dict(range = [0,1.6], autorange = False,
111106
showgrid = False, zeroline = False,
112107
linecolor = 'black', showticklabels = False,
113108
ticks = '' )
114109

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),
116111
xaxis = axis_template,
117112
yaxis = axis_template,
118113
showlegend = False,
119114
width = 700, height = 700,
120115
autosize = False )
121116

122-
figure = dict(data=[s, hm],layout=layout)
123-
124-
py.iplot(figure, filename='golden spiral', height=750)
125-
117+
fig.show()
126118
```
127119

128120
### Heatmap with Datetime Axis
129121

130122
```python
131-
123+
import plotly.graph_objects as go
132124
import datetime
133125
import numpy as np
134-
import plotly.plotly as py
135-
import plotly.graph_objs as go
136126

137127
programmers = ['Alex','Nicole','Sara','Etienne','Chelsea','Jody','Marianne']
138128

139129
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(
152134
z=z,
153-
x=date_list,
135+
x=dates,
154136
y=programmers,
155-
colorscale='Viridis',
156-
)
157-
]
137+
colorscale='Viridis'))
158138

159-
layout = go.Layout(
139+
fig.update_layout(
160140
title='GitHub commits per day',
161-
xaxis = dict(ticks='', nticks=36),
162-
yaxis = dict(ticks='' )
163-
)
141+
xaxis_nticks=36)
164142

165-
fig = go.Figure(data=data, layout=layout)
166-
py.iplot(fig, filename='datetime-heatmap')
143+
fig.show()
167144
```
168145

169146
### Dash Example
@@ -183,28 +160,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-heatmapplot/code", width=
183160

184161
#### Reference
185162
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

Comments
 (0)