In [1]:
import pandas as pd
from pandas import *
import matplotlib.pyplot as plt
%matplotlib inline

First, let's get some data.


In [2]:
data = pd.read_csv("our_heatmapdata.csv", index_col = False, header = False)
data.columns = ['A','B','C']

In [3]:
data


Out[3]:
A B C
0 0.1 0.2 0.857811
1 0.1 0.3 0.341262
2 0.1 0.4 0.156494
3 0.1 0.5 0.085529
4 0.1 0.6 0.053923
5 0.1 0.7 0.036073
6 0.1 0.8 0.026367
7 0.1 0.9 0.020581
8 0.1 1.0 0.017282
9 0.2 0.1 0.999239
10 0.2 0.2 0.994325
11 0.2 0.3 0.951304
12 0.2 0.4 0.841876
13 0.2 0.5 0.651873
14 0.2 0.6 0.417154
15 0.2 0.7 0.316169
16 0.2 0.8 0.244828
17 0.2 0.9 0.198894
18 0.2 1.0 0.173303
19 0.3 0.1 0.999900
20 0.3 0.2 0.999375
21 0.3 0.3 0.993243
22 0.3 0.4 0.970860
23 0.3 0.5 0.929798
24 0.3 0.6 0.868380
25 0.3 0.7 0.786981
26 0.3 0.8 0.703986
27 0.3 0.9 0.587173
28 0.3 1.0 0.532679
29 0.4 0.1 0.999960
... ... ... ...
69 0.8 0.1 0.999997
70 0.8 0.2 0.999953
71 0.8 0.3 0.999806
72 0.8 0.4 0.999227
73 0.8 0.5 0.997988
74 0.8 0.6 0.995626
75 0.8 0.7 0.992016
76 0.8 0.8 0.986893
77 0.8 0.9 0.980924
78 0.8 1.0 0.977893
79 0.9 0.1 0.999998
80 0.9 0.2 0.999960
81 0.9 0.3 0.999850
82 0.9 0.4 0.999515
83 0.9 0.5 0.998556
84 0.9 0.6 0.997382
85 0.9 0.7 0.993815
86 0.9 0.8 0.989667
87 0.9 0.9 0.987051
88 0.9 1.0 0.963459
89 1.0 0.1 0.999998
90 1.0 0.2 0.999965
91 1.0 0.3 0.999865
92 1.0 0.4 0.999560
93 1.0 0.5 0.998756
94 1.0 0.6 0.997313
95 1.0 0.7 0.995736
96 1.0 0.8 0.992309
97 1.0 0.9 0.989186
98 1.0 1.0 0.986302

99 rows × 3 columns

If you're like me, generally, you store data one variable per column. This isn't ideal for heatmaps. Matplotlib's heatmap assumes data have one variable down the x-axis of the spreadsheet and one along the top, or y-axis. Pandas can melt our data to be in this format.


In [6]:
h_data = data.pivot(index='A', columns='B', values='C')
h_data.ix[0.1,0.1] = .96
h_data


Out[6]:
B 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
A
0.1 0.960000 0.857811 0.341262 0.156494 0.085529 0.053923 0.036073 0.026367 0.020581 0.017282
0.2 0.999239 0.994325 0.951304 0.841876 0.651873 0.417154 0.316169 0.244828 0.198894 0.173303
0.3 0.999900 0.999375 0.993243 0.970860 0.929798 0.868380 0.786981 0.703986 0.587173 0.532679
0.4 0.999960 0.999792 0.998151 0.991522 0.976919 0.952405 0.924190 0.906039 0.861443 0.828445
0.5 0.999993 0.999887 0.999259 0.996801 0.990500 0.979818 0.967424 0.948895 0.927059 0.904931
0.6 0.999997 0.999928 0.999583 0.998439 0.995260 0.989324 0.985898 0.972025 0.958221 0.939646
0.7 0.999996 0.999949 0.999751 0.999018 0.996731 0.993216 0.988813 0.982332 0.975513 0.968044
0.8 0.999997 0.999953 0.999806 0.999227 0.997988 0.995626 0.992016 0.986893 0.980924 0.977893
0.9 0.999998 0.999960 0.999850 0.999515 0.998556 0.997382 0.993815 0.989667 0.987051 0.963459
1.0 0.999998 0.999965 0.999865 0.999560 0.998756 0.997313 0.995736 0.992309 0.989186 0.986302

In [7]:
plt.pcolor(h_data,cmap=plt.cm.winter,edgecolors='k')
plt.xticks(np.arange(0.5, len(h_data.columns), 1), h_data.columns)
plt.yticks(np.arange(0.5, len(h_data.index), 1), h_data.index)
cbar = plt.colorbar()
plt.show()


And heatmap. As you can se, our data has a very sharp switch point (i.e. values tend to be very high support or low support), with few in between.


In [9]:
plt.savefig('optimized')


<matplotlib.figure.Figure at 0xac25192c>

Copyright (c) <2014> <April Wright, wright.aprilm@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


In [ ]: