To graph multiple datasets (e.g., data1, data4, and data8), assuming these are already loaded as NumPy arrays similar to how data is loaded in your original code, you can modify your script to plot all of them on the same graph. This involves:
- Subsetting each dataset: Just like you subset the original
data, you'll subset data1, data4, and data8 based on x_start and x_end.
- Plotting multiple lines: Use
plt.plot() for each dataset, assigning different colors and labels to distinguish them.
- Handling the y-axis for rectangles: The original code uses
subset.max() * 1.05 to set the height of the highlighted regions. With multiple datasets, you should use the global maximum across all subsets to ensure the rectangles are scaled appropriately for the entire plot.
- Legend updates: The existing code already handles deduplicating legend entries, so it will automatically include the new lines along with the regions.
Here's the modified code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import gzip # If you still need to load one dataset, but assuming data1, data4, data8 exist
# Assuming data1, data4, and data8 are already loaded as NumPy arrays
# For example:
# with gzip.open('data1_file.gz', 'rt') as f1: data1 = np.loadtxt(f1)
# with gzip.open('data4_file.gz', 'rt') as f4: data4 = np.loadtxt(f4)
# with gzip.open('data8_file.gz', 'rt') as f8: data8 = np.loadtxt(f8)
# ---------- Prep x-axis and subset ----------
x_start, x_end = 4315509-50, 4315958+50
x = np.arange(x_start, x_end)
# Subset each dataset
subset1 = data1[x_start:x_end] # Subset for data1
subset4 = data4[x_start:x_end] # Subset for data4
subset8 = data8[x_start:x_end] # Subset for data8
# Calculate global maximum for rectangle height (across all subsets)
global_max_coverage = max(subset1.max(), subset4.max(), subset8.max()) * 1.05
# ---------- Basic plot ----------
plt.figure(figsize=(12, 6))
# Plot each dataset as a separate line
plt.plot(x, subset1, color='blue', linewidth=1, label='Data1') # Blue for data1
plt.plot(x, subset4, color='red', linewidth=1, label='Data4') # Red for data4
plt.plot(x, subset8, color='green', linewidth=1, label='Data8') # Green for data8
# ---------- Regions to highlight (alpha = 0.85) ----------
regions = [
{'start': 4315509, 'end': 4315958, 'color': 'lightgreen', 'label': 'ildP'},
#{'start': 1258678, 'end': 1258762, 'color': 'lightblue', 'label': 'Promoter ychHp'},
#{'start': 3913850, 'end': 3913876, 'color': 'khaki', 'label': 'TIR'},
#{'start': 3913803, 'end': 3913858, 'color': 'plum', 'label': 'BCD'},
#{'start': 1258738, 'end': 1258762, 'color': 'red', 'label': 'Transcription start'},
]
ax = plt.gca()
for r in regions:
ax.add_patch(
Rectangle(
(r['start'], 0),
r['end'] - r['start'],
global_max_coverage, # Use the global maximum here
facecolor=r['color'],
alpha=0.85,
label=r['label']
)
)
# ---------- Cosmetics ----------
plt.ylim(bottom=0)
plt.ticklabel_format(style='plain', axis='x', useOffset=False)
plt.xticks(np.arange(x_start, x_end+1, 1000), rotation=45)
plt.xlabel('Position')
plt.ylabel('Coverage')
plt.title('Coverage across genetic elements')
# Deduplicate legend entries (this will include both the lines and regions)
handles, labels = ax.get_legend_handles_labels()
unique_labels = dict(zip(labels, handles)) # Create a dictionary for unique entries
plt.legend(unique_labels.values(), unique_labels.keys(), loc='upper right')
plt.tight_layout()
plt.show()