Here's a Python code snippet that uses matplotlib and pandas to create a bar chart from your numpy array and plot the average for each gene from the csv file.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Load the csv file
genes = pd.read_csv('genes.csv', sep=';')
# Calculate the average for each gene
gene_averages = []
for index, row in genes.iterrows():
start = row['starting bp'] - 1 # subtract 1 because python starts counting from 0
end = row['ending bp']
gene_average = np.mean(data[start:end])
gene_averages.append(gene_average)
# Create a bar chart
plt.figure(figsize=(10, 6)) # set the figure size
plt.bar(range(len(data)), data, label='Data')
# Plot the averages of genes
for index, row in genes.iterrows():
start = row['starting bp'] - 1 # subtract 1 because python starts counting from 0
end = row['ending bp']
plt.plot([start, end], [gene_averages[index], gene_averages[index]], 'r', linewidth=2)
# Set the x-axis ticks
plt.xticks(np.arange(0, len(data), 500000), [f'{i/1000000:.1f}M' for i in np.arange(0, len(data), 500000)])
# Set the title and labels
plt.title('Bar Chart of Data with Gene Averages')
plt.xlabel('Position (M)')
plt.ylabel('Value')
plt.legend()
# Show the plot
plt.show()
This code first loads the csv file into a pandas DataFrame. Then it calculates the average for each gene by slicing the numpy array data based on the start and end positions of each gene. It creates a bar chart of the numpy array data and plots the averages of genes as horizontal lines in red. Finally, it sets the x-axis ticks to be every 0.5M and displays the plot.