Below is one way to overlay three coverage‐vectors (data1, data4, data8) on the same axes while keeping your “green‐box” highlight. The key steps are:
- Compute your common x‐axis (
x_start, x_end, x = np.arange(...)).
- Put your datasets (and their labels/colors) into a list or dict.
- Loop over that list, subset each dataset and call
plt.plot(...).
- Draw your shaded regions only once.
- Tidy up legend/labels.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# Assume data1, data4, data8 already loaded as 1D np.arrays of the same length
# e.g. data1 = data; data4 = np.loadtxt(...); etc.
# 1) Define the x window
x_start, x_end = 4315509-50, 4315958+50
x = np.arange(x_start, x_end)
# 2) Pack datasets, labels, colors
datasets = [
(data1, 'Sample 1', 'black'),
(data4, 'Sample 4', 'tab:blue'),
(data8, 'Sample 8', 'tab:red'),
]
# 3) Start figure
plt.figure(figsize=(12, 6))
ax = plt.gca()
# 4) Plot each dataset
for data, label, color in datasets:
subset = data[x_start:x_end]
ax.plot(x, subset,
label=label,
color=color,
linewidth=1)
# 5) Highlight region(s)
regions = [
{'start': 4315509, 'end': 4315958, 'color': 'lightgreen', 'label': 'ildP'}
]
ymax = max(d[x_start:x_end].max() for d,_,_ in datasets) * 1.05
for r in regions:
ax.add_patch(Rectangle(
(r['start'], 0),
r['end'] - r['start'],
ymax,
facecolor=r['color'],
alpha=0.85,
label=r['label']
))
# 6) Cosmetics
ax.set_xlim(x_start, x_end)
ax.set_ylim(0, ymax)
ax.ticklabel_format(style='plain', axis='x', useOffset=False)
ax.set_xticks(np.arange(x_start, x_end+1, 1000))
ax.set_xticklabels(np.arange(x_start, x_end+1, 1000), rotation=45)
ax.set_xlabel('Position')
ax.set_ylabel('Coverage')
ax.set_title('Coverage across genetic elements')
# Deduplicate legend entries
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
ax.legend(by_label.values(), by_label.keys(), loc='upper right')
plt.tight_layout()
plt.show()
— That’s it! You now get three overlaid coverage curves plus your highlighted region.
Tips / next steps
- If you have many samples, consider using a color map (e.g.
plt.cm.viridis) and generating a color for each line.
- You can also read all your files programmatically and build
datasets in a loop.
- If you’d like interactive zoom or hover‐tooltips, check out Plotly or Bokeh.
Let me know if you need help customizing colors, line styles, or adding more regions! 🚀