This notebook is part of smFRET burst analysis software FRETBursts.
In this notebook shows how to plot different styles of μs-ALEX histograms and $E$ and $S$ marginal distributions. For a complete tutorial on burst analysis see [FRETBursts - us-ALEX smFRET burst analysis](FRETBursts - us-ALEX smFRET burst analysis.ipynb).
from fretbursts import *
- Optimized (cython) burst search loaded. - Optimized (cython) photon counting loaded. -------------------------------------------------------------- You are running FRETBursts (version 0.7+46.ge31fadb.dirty). If you use this software please cite the following paper: FRETBursts: An Open Source Toolkit for Analysis of Freely-Diffusing Single-Molecule FRET Ingargiola et al. (2016). http://dx.doi.org/10.1371/journal.pone.0160716 --------------------------------------------------------------
sns = init_notebook(apionly=True)
print('seaborn version: ', sns.__version__)
seaborn version: 0.11.2
# Tweak here matplotlib style
import matplotlib as mpl
mpl.rcParams['font.sans-serif'].insert(0, 'Arial')
mpl.rcParams['font.size'] = 12
%config InlineBackend.figure_format = 'retina'
url = 'http://files.figshare.com/2182601/0023uLRpitc_NTP_20dT_0.5GndCl.hdf5'
download_file(url, save_dir='./data')
full_fname = "./data/0023uLRpitc_NTP_20dT_0.5GndCl.hdf5"
d = loader.photon_hdf5(full_fname)
loader.alex_apply_period(d)
d.calc_bg(bg.exp_fit, time_s=1000, tail_min_us=(800, 4000, 1500, 1000, 3000))
d.burst_search(L=10, m=10, F=6)
ds = d.select_bursts(select_bursts.size, add_naa=True, th1=30)
URL: http://files.figshare.com/2182601/0023uLRpitc_NTP_20dT_0.5GndCl.hdf5 File: 0023uLRpitc_NTP_20dT_0.5GndCl.hdf5 File already on disk: /home/paul/Disk/Python/OpenSMFS/FRETBursts_notebooks/notebooks/data/0023uLRpitc_NTP_20dT_0.5GndCl.hdf5 Delete it to re-download. # Total photons (after ALEX selection): 2,259,522 # D photons in D+A excitation periods: 721,537 # A photons in D+A excitation periods: 1,537,985 # D+A photons in D excitation period: 1,434,842 # D+A photons in A excitation period: 824,680 - Calculating BG rates ... get bg th arrays Channel 0 [DONE] - Performing burst search (verbose=False) ...[DONE] - Calculating burst periods ...[DONE] - Counting D and A ph and calculating FRET ... - Applying background correction. [DONE Counting D/A]
The alex_jointplot
function allows plotting an ALEX histogram with marginals.
This is how it looks by default:
alex_jointplot(ds);
<class 'matplotlib.figure.Figure'>
The inner plot in an hexbin plot, basically a 2D histogram with hexagonal bins. This kind of histograms resembles a scatter plot when sample size is small, and is immune from grid artifacts typical of rectangular grids. For more info for hexbin see this document.
The marginal plots are histograms with an overlay KDE plot. The same FRETBursts function that plots standalone E and S histograms is used here to plot the marginals in the joint plot.
Below I show how to customize appearance and type of this plot.
By default the colormap range is computed on the range S=[0.2, 0.8], so that the FRET populations (S ~ 0.5) have more contrast.
To normalize the colormap to the whole data use the vmax
argument:
alex_jointplot(ds, vmax_fret=False);
<class 'matplotlib.figure.Figure'>
alex_jointplot(ds, vmax_fret=False, marginal_color=8);
<class 'matplotlib.figure.Figure'>
alex_jointplot(ds, vmax_fret=False, marginal_color=7);
<class 'matplotlib.figure.Figure'>
alex_jointplot(ds, kind='kde');
<class 'matplotlib.figure.Figure'>
Or you can manually choose the max value mapped by the colormap (vmax
):
alex_jointplot(ds, vmax=40);
<class 'matplotlib.figure.Figure'>
Changing the colormap will affect both inner and marginal plots:
alex_jointplot(ds, cmap='plasma');
<class 'matplotlib.figure.Figure'>
To pick a different color from the colormap for the marginal histograms use histcolor_id
:
alex_jointplot(ds, cmap='plasma', marginal_color=83);
<class 'matplotlib.figure.Figure'>
alex_jointplot(ds, kind='scatter');
<class 'matplotlib.figure.Figure'>
alex_jointplot(ds, kind='kde');
<class 'matplotlib.figure.Figure'>
dsf = ds.select_bursts(select_bursts.naa, th1=40)
alex_jointplot(dsf, kind='kde',
joint_kws={'fill': True, 'levels': 12, 'bw': 0.04});
/home/paul/anaconda3/envs/Py38/lib/python3.8/site-packages/seaborn/distributions.py:1699: FutureWarning: The `bw` parameter is deprecated in favor of `bw_method` and `bw_adjust`. Using 0.04 for `bw_method`, but please see the docs for the new parameters and update your code. warnings.warn(msg, FutureWarning)
<class 'matplotlib.figure.Figure'>
Finally, we can plot only the hexbin 2D histogram without marginals:
plt.figure(figsize=(5,5))
hexbin_alex(ds)
You can get an handle of the different axes in the figure for layout customization:
g = alex_jointplot(ds)
g.axes[1].grid(False)
g.axes[2].grid(False)
g.axes[0].set_xlim(-0.1, 1.1)
g.axes[0].set_ylim(-0.1, 1.1)
<class 'matplotlib.figure.Figure'>
(-0.1, 1.1)
alex_jointplot
returns g
which is a matplotlib figure. The different axes can be accesed through the list inside of g.axes
. g.axes[0]
is the central joint plot, g.axes[1]
is the marginal histogram of the x-axis, and g.axes[2]
is the marginal histogram of the y-axis.
g = alex_jointplot(ds)
g.axes[1].grid(False)
g.axes[2].grid(False)
g.axes[0].set_xlim(-0.19, 1.19)
g.axes[0].set_ylim(-0.19, 1.19)
plt.subplots_adjust(wspace=0, hspace=0)
g.axes[1].spines['bottom'].set_visible(True)
g.axes[1].spines['left'].set_visible(True)
g.axes[2].tick_params(reset=True, bottom=True, top=False, right=False, labelleft=False)
g.axes[1].tick_params(reset=True, left=True, top=False, right=False, labelbottom=False)
<class 'matplotlib.figure.Figure'>
g = alex_jointplot(ds)
g.axes[1].grid(False)
g.axes[2].grid(False)
g.axes[0].set_xlim(-0.19, 1.19)
g.axes[0].set_ylim(-0.19, 1.19)
plt.subplots_adjust(wspace=0, hspace=0)
g.axes[2].tick_params(reset=True, bottom=True, top=False, right=False, labelleft=False)
g.axes[1].tick_params(reset=True, left=True, top=False, right=False, labelbottom=False)
<class 'matplotlib.figure.Figure'>
g = alex_jointplot(ds)
g.axes[1].grid(False, axis='x')
g.axes[1].grid(False, axis='y')
g.axes[0].set_xlim(-0.19, 1.19)
g.axes[0].set_ylim(-0.19, 1.19)
plt.subplots_adjust(wspace=0, hspace=0)
<class 'matplotlib.figure.Figure'>
Additional arguments can be passed to the inner or marginal plots passing
a dictionary to joint_kws
and marginal_kws
respectively.
The marginal plots are created by hist_burst_data
which is the same function used to plot standalone E and S histograms
in FRETBursts.
For example, we can remove the KDE overlay like this:
alex_jointplot(ds, marginal_kws={'show_kde': False});
<class 'matplotlib.figure.Figure'>
from ipywidgets import widgets, interact, interactive, fixed
from IPython.display import display, display_png, display_svg, clear_output
from IPython.core.pylabtools import print_figure
cmaps = ['viridis', 'plasma', 'inferno', 'magma',
'afmhot', 'Blues', 'BuGn', 'BuPu', 'GnBu', 'YlGnBu',
'coolwarm', 'RdYlBu', 'RdYlGn', 'Spectral',]# 'icefire'] uncomment if using seaborn 0.8
@interact(overlay = widgets.RadioButtons(options=['fit model', 'KDE'], value='KDE'),
binwidth = widgets.FloatText(value=0.03, min=0.01, max=1),
bandwidth = widgets.FloatText(value=0.03, min=0.01, max=1),
gridsize = (10, 100),
min_size=(10, 500, 5),
cmap=widgets.Dropdown(value='Spectral', options=cmaps),
reverse_cmap = True,
vmax_fret = True,
)
def plot_(min_size=50, overlay='KDE', binwidth=0.03, bandwidth=0.03,
gridsize=50, cmap='Spectral', reverse_cmap=False,
vmax_fret=True):
dx = d.select_bursts(select_bursts.size, add_naa=True, th1=min_size)
bext.bursts_fitter(dx, 'E', binwidth=binwidth, bandwidth=bandwidth,
model=mfit.factory_three_gaussians())
bext.bursts_fitter(dx, 'S', binwidth=binwidth, bandwidth=bandwidth,
model=mfit.factory_two_gaussians())
if reverse_cmap: cmap += '_r'
if binwidth < 0.01: binwidth = 0.01
if bandwidth < 0.01: bandwidth = 0.01
if overlay == 'fit model':
marginal_kws = dict(binwidth=binwidth, show_model=True, pdf=True,
show_kde=False)
else:
marginal_kws = dict(binwidth=binwidth, show_kde=True,
bandwidth=bandwidth)
alex_jointplot(dx, cmap=cmap, gridsize=gridsize, vmax_fret=vmax_fret,
marginal_kws=marginal_kws,)
fig = gcf()
plt.close()
display(fig)
interactive(children=(IntSlider(value=50, description='min_size', max=500, min=10, step=5), RadioButtons(descr…