In [1]:
# import matplotlib as mpl then mpl.use('TkAgg')
# import matplotlib as mpl
# # mpl.use('TkAgg')
%matplotlib inline
%pylab inline
import matplotlib.pyplot as plt
import numpy as np
import mpld3
from mpld3 import plugins

mpld3.enable_notebook()
Populating the interactive namespace from numpy and matplotlib
In [2]:
################################ LOAD DATA #####################################

#list format
lva_list = []
with open('/Users/Emily/dropbox/ambient_happ/labMTvsAmb.txt') as f:
    lines = f.readlines()
    for line in lines:
        lva_list.append(line.strip().split())

words = [lva_list[i][0][0:-1] for i in range(len(lva_list))]
lab = [float(lva_list[i][1][0:-1]) for i in range(len(lva_list))]
amb = [float(lva_list[i][2]) for i in range(len(lva_list))]
In [3]:
################################ MAKE PLOT #####################################
pylab.rcParams['figure.figsize'] = (10.0, 6.0)
N = 10222
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
ax.grid(color='white', linestyle='solid')

scatter = ax.scatter(lab[:N],
                     amb[:N],
                     #c=np.random.random(size=N),
                     #s = 1000 * np.random.random(size=N),
                     alpha=0.3,
                     cmap=plt.cm.jet)
                     
# fit with np.polyfit
idx = np.isfinite(amb) #exclude nans
m, b = np.polyfit(np.array(lab)[idx], np.array(amb)[idx], 1)
plt.plot(np.array(lab)[idx], m*np.array(lab)[idx] + b, 'r-')

eqn = 'y = '+str(round(m,2))+'x+'+str(round(b,2))

#ax.set_title("D3 Scatter Plot (with tooltips!)", size=20)
plt.legend([eqn],loc='upper left')
plt.xlabel("labMT score")
plt.ylabel("ambient score")

tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=words[:N])
mpld3.plugins.connect(fig, tooltip)
# mpld3.show()