I'll help you create both plots using the existing data. Here's how to create them:
For the bar chart with the numpy array:
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plt.bar(range(len(data)), data)
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(500000)) # 0.5M intervals
plt.xlabel('Position')
plt.ylabel('Value')
plt.title('Bar Chart')
plt.show()
For the gene data plot:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Read the CSV file
df = pd.read_csv('gene.csv', sep=';')
# Initialize lists for plotting
positions = []
averages = []
colors = []
labels = []
# Process first intergenic region (if exists)
if df['starting bp'].iloc[0] > 1:
start = 0
end = df['starting bp'].iloc[0] - 1
avg = np.mean(data[start:end])
positions.append((start + end) / 2)
averages.append(avg)
colors.append('blue')
labels.append('Intergenic')
# Process genes and intergenic regions
for i in range(len(df)):
# Current gene
start = df['starting bp'].iloc[i] - 1 # Convert to 0-based indexing
end = df['ending bp'].iloc[i] - 1
avg = np.mean(data[start:end])
positions.append((start + end) / 2)
averages.append(avg)
colors.append('red')
labels.append('Gene')
# Intergenic region (if not last gene)
if i < len(df) - 1:
start = df['ending bp'].iloc[i] - 1
end = df['starting bp'].iloc[i + 1] - 1
avg = np.mean(data[start:end])
positions.append((start + end) / 2)
averages.append(avg)
colors.append('blue')
labels.append('Intergenic')
plt.figure(figsize=(12, 6))
plt.scatter(positions, averages, c=colors)
plt.xlabel('Position')
plt.ylabel('Average Value')
plt.title('Gene and Intergenic Region Averages')
# Add legend
from matplotlib.lines import Line2D
legend_elements = [Line2D([0], [0], marker='o', color='red', label='Gene', markersize=10),
Line2D([0], [0], marker='o', color='blue', label='Intergenic', markersize=10)]
plt.legend(handles=legend_elements)
plt.show()
This code will:
- Create a bar chart from your existing numpy array with 0.5M interval marks
- Create a scatter plot showing the average values for genes and intergenic regions
- Color genes in red and intergenic regions in blue
- Add appropriate labels and legend
- Account for 0-based indexing in Python vs 1-based indexing in the gene positions
The plots will show the distribution of your data and the average values at each gene and intergenic region position.