Skip to content

Plotting API

The Plotting API provides built-in plotting functions. For basic usage, you can generate plots dierctly via Nanocompore's command line interface (Plotting guide). However, if you want to use the plotting functions programatically or want to customize them beyond the basic parameters provided by the command line interface, you can use the API. Nanocompore uses Seaborn, so all functions produce Matplotlib figures that you can modify.

For example:

>>> from nanocompore.api import load_config
>>> from nanocompore.plotting import plot_coverage

# Load the YAML configuration file to a Config object.
>>> config = load_config('analysis.yaml')

# Get a coverage figure for a given transcript.
>>> ref_id = 'ENST00000464651.1|ENSG00000166136.16|OTTHUMG00000019346.4|OTTHUMT00000051221.1|NDUFB8-204|NDUFB8|390|retained_intron|'
>>> fig = plot_coverage(config, ref_id)

# We can now manipulate the figure to customize it
# beyond the parametrization of provided by the API.
# E.g. we can add a title:
>>> fig.axes[0].set_title('Coverage of NDUF8B-204')

# Then we update the layout and save the figure:
>>> fig.tight_layout()
>>> fig.savefig('NDUF8B_coverage.png')

Reference

plot_coverage(config, reference, start=None, end=None, figsize=(30, 10), split_samples=False, palette='Dark2')

Plot the read coverage over a reference for all samples analysed. Note that this would plot the input coverage before applying any filtering that Nanocompore does before the comparison.

Parameters:

Name Type Description Default
config Config

The configuration object for the run.

required
reference str

Transcript reference.

required
start Union[int, None]

Start of the region that will be plotted.

None
end Union[int, None]

End of the region that will be plotted.

None
figsize tuple[int, int]

Size of the figure.

(30, 10)
split_samples bool

If True, all samples would be plotted separately. By default it's False and the samples are grouped by condition.

False
palette str

Color palette to use.

'Dark2'

Returns:

Type Description
Union[Figure, SubFigure, None]

Figure with the coverage plot.

Source code in nanocompore/plotting.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
def plot_coverage(config: Config,
                  reference: str,
                  start: Union[int, None]=None,
                  end: Union[int, None]=None,
                  figsize: tuple[int, int]=(30, 10),
                  split_samples: bool=False,
                  palette: str='Dark2') -> Union[Figure, SubFigure, None]:
    """
    Plot the read coverage over a reference for all samples analysed.
    Note that this would plot the input coverage before applying
    any filtering that Nanocompore does before the comparison.

    Parameters
    ----------
    config : Config
        The configuration object for the run.
    reference : str
        Transcript reference.
    start : Union[int, None]
        Start of the region that will be plotted.
    end : Union[int, None]
        End of the region that will be plotted.
    figsize : tuple[int, int]
        Size of the figure.
    split_samples : bool
        If True, all samples would be plotted separately.
        By default it's False and the samples are grouped
        by condition.
    palette : str
        Color palette to use.

    Returns
    -------
    Union[Figure, SubFigure, None]
        Figure with the coverage plot.

    """
    data, reads, samples, conditions = get_reads(config, reference)
    positions = np.arange(data.shape[1])[start:end]
    data = data[:, start:end, 0]

    if split_samples:
        group_var = 'sample'
        groups = np.array(samples)
    else:
        group_var = 'condition'
        groups = np.array(conditions)

    covs = {}
    all_group_labels = np.unique(groups)
    for group in all_group_labels:
        covs[group] = np.sum(~np.isnan(data[groups == group, :]), axis=0)
    covs['pos'] = positions
    covs = pd.DataFrame(covs)
    covs = pd.melt(covs,
                   id_vars=['pos'],
                   value_vars=all_group_labels,
                   var_name=group_var,
                   value_name='cov')

    fig, ax = plt.subplots(figsize=figsize)

    sns.lineplot(covs, x='pos', y='cov', hue=group_var, palette=palette, ax=ax)
    ax.axhline(y=config.get_min_coverage(), linestyle='--', color='grey', label='Minimum coverage')

    plt.legend()
    sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
    plt.tight_layout()

    ax.set_xlabel('Reference position')
    ax.set_ylabel('Coverage')

    return fig

plot_gmm(config, reference, position, figsize=(10, 10), point_size=20, xlim=(None, None), ylim=(None, None), gmm_levels=4, palette='Dark2', point_palette=None, gmm_palette=None)

Plot the GMM fitted by Nanocompore for a given position.

Parameters:

Name Type Description Default
config Config

The configuration object for the run.

required
reference str

Transcript reference.

required
position int

0-based index on the reference indicating the position.

required
figsize tuple[int, int]

Size of the figure.

(10, 10)
point_size int

Size of the points.

20
xlim Union[tuple[Union[int, None], Union[int, None]], None]

Limits of the x-axis.

(None, None)
ylim Union[tuple[Union[int, None], Union[int, None]], None]

Limits of the y-axis.

(None, None)
gmm_levels int

How many levels of the GMM to show.

4
palette Union[str, None]

Palette that will be used to determine the colors of both points and the GMMs. If set, it will override point_palette and gmm_palette.

'Dark2'
point_palette Union[str, None]

Palette to use for the points.

None
gmm_palette Union[str, None]

Palette to use for the GMMs.

None

Returns:

Type Description
Union[Figure, SubFigure, None]

The resulting figure that will contain a 2D plot with the observations as points and the gaussians obtained from the GMM.

Source code in nanocompore/plotting.py
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
def plot_gmm(config: Config,
             reference: str,
             position: int,
             figsize: tuple[int, int]=(10, 10),
             point_size: int=20,
             xlim: Union[tuple[Union[int, None], Union[int, None]], None]=(None, None),
             ylim: Union[tuple[Union[int, None], Union[int, None]], None]=(None, None),
             gmm_levels: int=4,
             palette: Union[str, None]='Dark2',
             point_palette: Union[str, None]=None,
             gmm_palette: Union[str, None]=None) -> Union[Figure, SubFigure, None]:
    """
    Plot the GMM fitted by Nanocompore for a given position.

    Parameters
    ----------
    config : Config
        The configuration object for the run.
    reference : str
        Transcript reference.
    position : int
        0-based index on the reference indicating the position.
    figsize : tuple[int, int]
        Size of the figure.
    point_size : int
        Size of the points.
    xlim : Union[tuple[Union[int, None], Union[int, None]], None]
        Limits of the x-axis.
    ylim : Union[tuple[Union[int, None], Union[int, None]], None]
        Limits of the y-axis.
    gmm_levels : int
        How many levels of the GMM to show.
    palette : Union[str, None]
        Palette that will be used to determine the
        colors of both points and the GMMs. If set,
        it will override point_palette and gmm_palette.
    point_palette : Union[str, None]
        Palette to use for the points.
    gmm_palette : Union[str, None]
        Palette to use for the GMMs.

    Returns
    -------
    Union[Figure, SubFigure, None]
       The resulting figure that will contain a
       2D plot with the observations as points
       and the gaussians obtained from the GMM.
    """
    fasta_fh = Fasta(config.get_fasta_ref())
    ref_seq = str(fasta_fh[reference])

    transcript = Transcript(1, reference, ref_seq)

    if config.get_resquiggler() == UNCALLED4:
        worker_class = Uncalled4Worker
    else:
        worker_class = GenericWorker
    worker = worker_class(1, None, None, None, None, 'cpu', config)
    data, samples, conditions = worker._read_data(transcript)
    prepared_data = worker._prepare_data(data, samples, conditions)
    data, samples, conditions, _ = prepared_data
    data = data[[position], :, :]
    max_reads = worker._conf.get_downsample_high_coverage()
    data, samples, conditions = worker._downsample(
            data, samples, conditions, max_reads)

    std = nanstd(data, 1).unsqueeze(1)
    outliers = (((data - data.nanmean(1, keepdim=True)) / std).abs() > 3).any(2)
    data[outliers] = np.nan

    # Standardize the data
    std = nanstd(data, 1)
    data = (data - data.nanmean(1).unsqueeze(1)) / std.unsqueeze(1)

    gmm = GMM(n_components=2,
              device='cpu',
              random_seed=42,
              dtype=torch.float32)
    gmm.fit(data)

    # Means is a list with the means for each component.
    # The shape of each is (Points, Dims). We have a single point.
    c1_mean = gmm.means[0][0]
    c2_mean = gmm.means[1][0]

    # Covs is a list with the cov matrices for the components.
    # The shape of each is (Points, Dims, Dims).
    c1_cov = gmm.covs[0][0]
    c2_cov = gmm.covs[1][0]

    x1, y1 = np.random.multivariate_normal(c1_mean, c1_cov, 1000).T
    x2, y2 = np.random.multivariate_normal(c2_mean, c2_cov, 1000).T
    sampled_gaussians = pd.DataFrame(
        {'x': np.concatenate([x1, x2]),
         'y': np.concatenate([y1, y2]),
         'cluster': np.concatenate([np.full((1000,), 'Cluster 1'),
                                    np.full((1000,), 'Cluster 2')])})

    df = pd.DataFrame(data[0], columns=['intensity', 'dwell'])
    cond_labels = config.get_condition_labels()
    df['condition'] = [cond_labels[c] for c in conditions]

    if palette is not None:
        palette = {cond_labels[0]: plt.get_cmap(palette).colors[0],
                   cond_labels[1]: plt.get_cmap(palette).colors[1],
                   'Cluster 1': plt.get_cmap(palette).colors[2],
                   'Cluster 2': plt.get_cmap(palette).colors[3]}
    else:
        palette = {cond_labels[0]: plt.get_cmap(point_palette).colors[0],
                   cond_labels[1]: plt.get_cmap(point_palette).colors[1],
                   'Cluster 1': plt.get_cmap(gmm_palette).colors[0],
                   'Cluster 2': plt.get_cmap(gmm_palette).colors[1]}

    fig, ax = plt.subplots(figsize=figsize)
    if xlim is not None:
        ax.set_xlim(*xlim)
    if ylim is not None:
        ax.set_ylim(*ylim)
    if xlim is None and ylim is None:
        lims = (min(df.dwell.min(),
                    df.intensity.min(),
                    sampled_gaussians.x.min(),
                    sampled_gaussians.y.min()),
                max(df.dwell.max(),
                    df.intensity.max(),
                    sampled_gaussians.x.max(),
                    sampled_gaussians.y.max()))
        ax.set_xlim(*lims)
        ax.set_ylim(*lims)

    sns.kdeplot(sampled_gaussians,
                x='x',
                y='y',
                levels=gmm_levels,
                hue="cluster",
                palette=palette,
                ax=ax)
    sns.scatterplot(df,
                    x='dwell',
                    y='intensity',
                    hue='condition',
                    s=point_size,
                    palette=palette,
                    legend=False,
                    ax=ax)

    handles = [mpatches.Patch(facecolor=col, label=label)
               for label, col in palette.items()]
    plt.legend(handles=handles)

    ax.set_xlabel('Standardized log10(dwell)')
    ax.set_ylabel('Standardized intensity')
    return fig

plot_position(config, reference, position, figsize=(10, 10), point_size=20, xlim=(None, None), ylim=(None, None), show_kde=True, kde_levels=10, palette='Dark2')

Plot the signal data for a given position.

Parameters:

Name Type Description Default
config Config

The configuration object for the run.

required
reference str

Transcript reference.

required
position int

0-based index on the reference indicating the position.

required
figsize tuple[int, int]

Size of the figure.

(10, 10)
point_size int

Size of the points.

20
xlim Union[tuple[Union[int, None], Union[int, None]], None]

Limits of the x-axis.

(None, None)
ylim Union[tuple[Union[int, None], Union[int, None]], None]

Limits of the y-axis.

(None, None)
kde bool

Whether to show the KDEs.

required
kde_levels int

How many levels of the KDEs to show.

10
palette Union[str, None]

Palette that will be used to determine the colors of both points and the GMMs. If set, it will override point_palette and gmm_palette.

'Dark2'

Returns:

Type Description
Union[Figure, SubFigure, None]

The resulting figure that will contain a 2D plot with the observations as points and the gaussians obtained from the GMM.

Source code in nanocompore/plotting.py
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def plot_position(config: Config,
                  reference: str,
                  position: int,
                  figsize: tuple[int, int]=(10, 10),
                  point_size: int=20,
                  xlim: Union[tuple[Union[int, None], Union[int, None]], None]=(None, None),
                  ylim: Union[tuple[Union[int, None], Union[int, None]], None]=(None, None),
                  show_kde: bool=True,
                  kde_levels: int=10,
                  palette: Union[str, None]='Dark2') -> Union[Figure, SubFigure, None]:
    """
    Plot the signal data for a given position.

    Parameters
    ----------
    config : Config
        The configuration object for the run.
    reference : str
        Transcript reference.
    position : int
        0-based index on the reference indicating the position.
    figsize : tuple[int, int]
        Size of the figure.
    point_size : int
        Size of the points.
    xlim : Union[tuple[Union[int, None], Union[int, None]], None]
        Limits of the x-axis.
    ylim : Union[tuple[Union[int, None], Union[int, None]], None]
        Limits of the y-axis.
    kde : bool
        Whether to show the KDEs.
    kde_levels : int
        How many levels of the KDEs to show.
    palette : Union[str, None]
        Palette that will be used to determine the
        colors of both points and the GMMs. If set,
        it will override point_palette and gmm_palette.

    Returns
    -------
    Union[Figure, SubFigure, None]
       The resulting figure that will contain a
       2D plot with the observations as points
       and the gaussians obtained from the GMM.
    """
    fig, ax = plt.subplots(figsize=figsize)
    if xlim is not None:
        ax.set_xlim(*xlim)
    if ylim is not None:
        ax.set_ylim(*ylim)
    df = get_pos(config, reference, position)
    sns.scatterplot(df,
                    x='dwell',
                    y='intensity',
                    hue='condition',
                    s=point_size,
                    palette=palette,
                    ax=ax)
    if show_kde:
        sns.kdeplot(df,
                    x='dwell',
                    y='intensity',
                    levels=kde_levels,
                    hue="condition",
                    palette=palette,
                    ax=ax)

    ax.set_xlabel('Dwell time')
    ax.set_ylabel('Intensity')

    return fig

plot_pvalues(config, reference, start=None, end=None, kind='lineplot', threshold=0.01, figsize=(30, 10), tests=None, palette='Dark2')

Plot the p-values from the statistical tests performed in a Nanocompore run.

Parameters:

Name Type Description Default
config Config

The configuration object for the run.

required
reference str

Transcript reference.

required
start Union[int, None]

Start of the region that will be plotted.

None
end Union[int, None]

End of the region that will be plotted.

None
kind str

Kind of plot to make. The available options are: lineplot, barplot

'lineplot'
threshold Union[float, None]

If set, it will indicate the p-value threshold as a dashed horizontal line.

0.01
figsize tuple[int, int]

Size of the figure.

(30, 10)
tests Union[list[str], None]

List of tests to plot. The available options are: GMM, KS, TT, MW. If set to None (default) it would plot all tests that were listed in the configuration.

None
palette str

Color palette to use.

'Dark2'

Returns:

Type Description
Union[Figure, SubFigure, None]

The resulting figure that will contain the p-values for the specified tests in the provided region.

Source code in nanocompore/plotting.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def plot_pvalues(config: Config,
                 reference: str,
                 start: Union[int, None]=None,
                 end: Union[int, None]=None,
                 kind: str='lineplot',
                 threshold: Union[float, None]=0.01,
                 figsize: tuple[int, int]=(30, 10),
                 tests: Union[list[str], None]=None,
                 palette: str='Dark2') -> Union[Figure, SubFigure, None]:
    """
    Plot the p-values from the statistical tests
    performed in a Nanocompore run.

    Parameters
    ----------
    config : Config
        The configuration object for the run.
    reference : str
        Transcript reference.
    start : Union[int, None]
        Start of the region that will be plotted.
    end : Union[int, None]
        End of the region that will be plotted.
    kind : str
        Kind of plot to make. The available options
        are: lineplot, barplot
    threshold : Union[float, None]
        If set, it will indicate the p-value threshold
        as a dashed horizontal line.
    figsize : tuple[int, int]
        Size of the figure.
    tests : Union[list[str], None]
        List of tests to plot. The available options
        are: GMM, KS, TT, MW.
        If set to None (default) it would plot all
        tests that were listed in the configuration.
    palette : str
        Color palette to use.

    Returns
    -------
    Union[Figure, SubFigure, None]
       The resulting figure that will contain the
       p-values for the specified tests in the
       provided region.
    """
    db = ResultsDB(config)
    cols = []
    if tests is None:
        tests = config.get_comparison_methods()
    for test in tests:
        if test not in TEST_PVALUE_COLUMNS:
            raise KeyError(f"Test {test} not supported.")
        for col in TEST_PVALUE_COLUMNS[test]:
            cols.append(col)
    pvals = db.get_columns_for_ref(cols + ['kmer'], reference)
    pvals = pd.melt(pvals,
                    id_vars=['pos', 'kmer'],
                    value_vars=[c for c in pvals.columns if c != 'pos'],
                    var_name='test',
                    value_name='pval')
    pvals['pval'] = -np.log10(pvals.pval)
    pvals['kmer'] = pvals.kmer.apply(lambda k: decode_kmer(k, config.get_kit().len))
    if start is not None and end is not None:
        pvals = pvals[pvals.pos.between(start, end)]
    elif start is not None:
        pvals = pvals[pvals.pos >= start]
    elif end is not None:
        pvals = pvals[pvals.pos <= end]

    max_pos = pvals.pos.max()
    min_pos = pvals.pos.min()
    is_detailed_plot = max_pos - min_pos <= 50

    fig, ax = plt.subplots(figsize=figsize)

    if threshold is not None:
        ax.axhline(y=-np.log10(threshold), color='grey', linestyle='--', label=f'p-value = {threshold}')

    if kind == 'lineplot' and is_detailed_plot:
        sns.pointplot(pvals, x='pos', y='pval', hue='test', ax=ax)
    elif kind == 'lineplot':
        sns.lineplot(pvals, x='pos', y='pval', hue='test', ax=ax)
    elif kind == 'barplot':
        sns.barplot(pvals, x='pos', y='pval', hue='test', ax=ax)
    else:
        raise ValueError(f'Plot kind {kind} not supported.')

    ax.set_xlabel("Reference position")
    ax.set_ylabel("-log10(pvalue)")

    if is_detailed_plot:
        ax2 = ax.twiny()
        ax2.spines['bottom'].set_position(('axes', -0.18))
        ax2.spines['bottom'].set_visible(False)
        kmer_df = pvals.loc[:, ['pos', 'kmer']].drop_duplicates()
        ax2.set_xlim(min_pos - 0.5, max_pos + 0.5)
        ax2.set_xticks(kmer_df.pos)
        ax2.set_xticklabels(kmer_df.kmer, rotation=60)


    # plt.legend()
    # print(ax2.get_legend_handles_labels())
    # plt.legend(loc="upper left", bbox_to_anchor=(1, 1))
    sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
    plt.tight_layout()

    return fig

plot_signal(config, reference, start=None, end=None, kind='violinplot', figsize=(30, 10), split_samples=False, markersize=2, palette='Dark2')

Plot the raw signal values (intensity and dwell time) for the given reference and region. Note that this will plot all reads from the input files without applying all the filtering and downsampling that Nanocompore does.

Parameters:

Name Type Description Default
config Config

The configuration object for the run.

required
reference str

Transcript reference.

required
start Union[int, None]

Start of the region that will be plotted.

None
end Union[int, None]

End of the region that will be plotted.

None
kind str

Kind of plot to make. The available options are: lineplot, barplot

'violinplot'
figsize tuple[int, int]

Size of the figure.

(30, 10)
split_samples bool

If True, all samples would be plotted separately. By default it's False and the samples are grouped by condition.

False
markersize int

Size of the points (only used for the swarmplot).

2
palette str

Color palette to use.

'Dark2'

Returns:

Type Description
Union[Figure, SubFigure, None]

The resulting figure that will contain the intensity and log-dwell-time plots for the specified reference and region.

Source code in nanocompore/plotting.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
def plot_signal(config: Config,
                reference: str,
                start: Union[int, None]=None,
                end: Union[int, None]=None,
                kind: str='violinplot',
                figsize: tuple[int, int]=(30, 10),
                split_samples: bool=False,
                markersize: int=2,
                palette: str='Dark2') -> Union[Figure, SubFigure, None]:
    """
    Plot the raw signal values (intensity and dwell time)
    for the given reference and region. Note that this
    will plot all reads from the input files without
    applying all the filtering and downsampling that
    Nanocompore does.

    Parameters
    ----------
    config : Config
        The configuration object for the run.
    reference : str
        Transcript reference.
    start : Union[int, None]
        Start of the region that will be plotted.
    end : Union[int, None]
        End of the region that will be plotted.
    kind : str
        Kind of plot to make. The available options
        are: lineplot, barplot
    figsize : tuple[int, int]
        Size of the figure.
    split_samples : bool
        If True, all samples would be plotted separately.
        By default it's False and the samples are grouped
        by condition.
    markersize : int
        Size of the points (only used for the swarmplot).
    palette : str
        Color palette to use.

    Returns
    -------
    Union[Figure, SubFigure, None]
       The resulting figure that will contain the
       intensity and log-dwell-time plots for the
       specified reference and region.
    """
    data, _, samples, conditions = get_reads(config, reference)
    positions = np.arange(data.shape[1])

    data = data[:, start:end, :]
    positions = positions[start:end]

    nreads = data.shape[0]
    group_var = samples if split_samples else conditions
    group_var_name = 'Sample' if split_samples else 'Condition'
    long_data = []
    for r in range(nreads):
        for p, pos in enumerate(positions):
            long_data.append((group_var[r],
                              pos,
                              data[r, p, INTENSITY_POS],
                              np.log10(data[r, p, DWELL_POS])))
    df = pd.DataFrame(long_data, columns=[group_var_name, 'pos', 'intensity', 'dwell'])

    fig, ax = plt.subplots(2, 1, figsize=figsize)

    if kind == 'violinplot':
        sns.violinplot(df, x='pos', y='intensity', hue=group_var_name, ax=ax[0], inner='quart', split=not split_samples)
        sns.violinplot(df, x='pos', y='dwell', hue=group_var_name, ax=ax[1], inner='quart', split=not split_samples, legend=False)
    elif kind == 'swarmplot':
        sns.swarmplot(df, x='pos', y='intensity', hue=group_var_name, ax=ax[0], size=markersize, dodge=True)
        sns.swarmplot(df, x='pos', y='dwell', hue=group_var_name, ax=ax[1], size=markersize, dodge=True, legend=False)
    elif kind == 'boxenplot':
        sns.boxenplot(df, x='pos', y='intensity', hue=group_var_name, ax=ax[0])
        sns.boxenplot(df, x='pos', y='dwell', hue=group_var_name, ax=ax[1], legend=False)
    else:
        raise ValueError(f'Plot kind {kind} not supported.')

    ax[0].set_xlabel(None)
    ax[0].set_ylabel("intensity")
    ax[1].set_xlabel("Reference position")
    ax[1].set_ylabel("log10(dwell time)")

    sns.move_legend(ax[0], "upper left", bbox_to_anchor=(1, 1), markerscale=math.ceil(10/markersize))

    plt.tight_layout()

    return fig