Edinburgh-Genome-Foundry / DnaFeaturesViewer

:eye: Python library to plot DNA sequence features (e.g. from Genbank files)
https://edinburgh-genome-foundry.github.io/DnaFeaturesViewer/
MIT License
584 stars 90 forks source link

Labels overlap whan sharing axis #64

Open Ale-Rossi opened 2 years ago

Ale-Rossi commented 2 years ago

I tried to use plt.subplots to plot multiple sequences in a single figure, using the option sharex=True in order to keep the same scale for all the sequences. This means that shorter sequences get "squeezed" when drawn, and labels are closer together than they would be if they didn't share the x axis with the longer sequences. This is not taken into account and labels end up overlapping.

overlapping_labels

Thank you for your attention and thank you for this package, it is very well made and I really like the approach to coding shown in the documentation.

Please find attached the code and data used to generate this example.

overlapping_labels.zip

veghp commented 2 years ago

Thanks for bringing attention to this. I believe a solution would not be trivial, as the core algorithm that works on one plot and places the labels need to be made aware of the true size of the plot. A workaround is to set labels_spacing big enough to put each label on its own line:

graphic_records[0].labels_spacing = 100
fig, axes = plt.subplots(len(graphic_records), sharex=True, figsize=(10,5))

for record, ax in zip(graphic_records, axes):
    record.plot(ax=ax, annotate_inline=False, elevate_outline_annotations=False)

overlapping_labels

Zulko commented 2 years ago

What happens when you plot 2 sequences in a figure with share_x is:

If I remember correctly there is an easy fix which is to use x_lim so that all plots get plotted with the right x scale from the start:

record_1.plot(ax=ax[0], xlim=[0, 17000])
record_2.plot(ax=ax[1], xlim=[0, 17000])