diff --git a/mr/motion.py b/mr/motion.py index 4b6e04a..3acc2cf 100644 --- a/mr/motion.py +++ b/mr/motion.py @@ -111,6 +111,22 @@ def imsd(traj, mpp, fps, max_lagtime=100, statistic='msd'): results.index.name = 'lag time [s]' return results +def imsd_plot(imsd_results): + + from mpld3 import plugins, fig_to_d3 + fig, ax = plt.subplots() + labels = ['particle {0}'.format(i) for i in imsd_results.columns] + lines = ax.plot(imsd_results) + for line, label in zip(lines, labels): + plugins.connect(fig,mpld3.plugins.LineLabelTooltip(line,label)) + mpld3.fig_to_d3(fig) + + ax.set_title('Individual MSD Plots') + plt.xscale('log') + plt.yscale('log') + + return fig + def emsd(traj, mpp, fps, max_lagtime=100, detail=False): """Compute the mean squared displacements of an ensemble of probes. diff --git a/mr/plots.py b/mr/plots.py index f5588e0..51edecd 100644 --- a/mr/plots.py +++ b/mr/plots.py @@ -74,8 +74,7 @@ def wrapper(*args, **kwargs): return wrapper @make_axes -def plot_traj(traj, colorby='probe', mpp=1, label=False, superimpose=None, - cmap=mpl.cm.winter, ax=None): +def plot_traj(traj, colorby='probe', mpp=1, label=False, superimpose=None, cmap=mpl.cm.winter, ax=None): """Plot traces of trajectories for each probe. Optionally superimpose it on a frame from the video. @@ -88,8 +87,7 @@ def plot_traj(traj, colorby='probe', mpp=1, label=False, superimpose=None, superimpose : background image, default None cmap : This is only used in colorby='frame' mode. Default = mpl.cm.winter - ax : matplotlib axes object, defaults to current axes - + Returns ------- None @@ -97,7 +95,8 @@ def plot_traj(traj, colorby='probe', mpp=1, label=False, superimpose=None, if (superimpose is not None) and (mpp != 1): raise NotImplementedError("When superimposing over an image, you " + "must plot in units of pixels. Leave " + - "microns per pixel mpp=1.") + "microns per pixel mpp=1.") + fig, ax = plt.subplots(1, 1) # Axes labels if mpp == 1: ax.set_xlabel('x [px]') @@ -114,7 +113,18 @@ def plot_traj(traj, colorby='probe', mpp=1, label=False, superimpose=None, if colorby == 'probe': # Unstack probes into columns. unstacked = traj.set_index(['frame', 'probe']).unstack() - ax.plot(mpp*unstacked['x'], mpp*unstacked['y'], linewidth=1) + lines = ax.plot(mpp*unstacked['x'], mpp*unstacked['y'], linewidth=1) + + try: + from mpld3 import plugins,fig_to_d3 + except: + pass + else: + labels = ['particle {0}'.format(i) for i in traj.particle.unique()] + for line, number in zip(lines, labels): + plugins.connect(fig,mpld3.plugins.LineLabelTooltip(line,number)) + mpld3.fig_to_d3(fig) + if colorby == 'frame': # Read http://www.scipy.org/Cookbook/Matplotlib/MulticoloredLine from matplotlib.collections import LineCollection @@ -141,6 +151,7 @@ def plot_traj(traj, colorby='probe', mpp=1, label=False, superimpose=None, horizontalalignment='center', verticalalignment='center') ax.invert_yaxis() + return ax ptraj = plot_traj # convenience alias