Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mr/motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
23 changes: 17 additions & 6 deletions mr/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -88,16 +87,16 @@ 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
"""
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]')
Expand All @@ -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
Expand All @@ -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
Expand Down