Here's a Python script that reads your CSV file genes_averages.csv, processes the data, and generates a plot with averages of genes in red and intergenic regions in blue. The fill_between function is used to fill the area below the data points with the appropriate color.
import pandas as pd
import matplotlib.pyplot as plt
# Read the CSV file into a DataFrame
file_path = 'genes_averages.csv'
data = pd.read_csv(file_path, sep=';')
# Separate the data into genes and intergenic regions
genes = data[data['Gene name'].str.contains('gene')]
intergenic = data[data['Gene name'].str.contains('INTERGENIC')]
# Create a plot
plt.figure(figsize=(12, 6))
# Plot and fill for genes (red)
plt.plot(genes['starting bp'], genes['average'], color='red', label='Genes', marker='o')
plt.fill_between(genes['starting bp'], genes['average'], color='red', alpha=0.3)
# Plot and fill for intergenic regions (blue)
plt.plot(intergenic['starting bp'], intergenic['average'], color='blue', label='Intergenic Regions', marker='o')
plt.fill_between(intergenic['starting bp'], intergenic['average'], color='blue', alpha=0.3)
# Add labels, title, and legend
plt.xlabel('Starting Base Pair (bp)')
plt.ylabel('Average')
plt.title('Averages of Genes and Intergenic Regions')
plt.legend()
# Show the plot
plt.grid(True)
plt.tight_layout()
plt.show()
Explanation of the Script:
-
Reading the CSV File:
- The
pd.read_csv function reads the CSV file into a Pandas DataFrame. The separator is specified as ;.
-
Separating Genes and Intergenic Regions:
- Rows corresponding to genes are identified by checking if the
Gene name column contains the word "gene".
- Rows corresponding to intergenic regions are identified by checking if the
Gene name column contains "INTERGENIC".
-
Plotting:
- For genes, the data points are plotted in red (
color='red'), and the area below the points is filled with a semi-transparent red color using fill_between.
- For intergenic regions, the data points are plotted in blue (
color='blue'), and the area below the points is filled with a semi-transparent blue color using fill_between.
-
Customizing the Plot:
- Labels for the x-axis and y-axis are added.
- A title is added to describe the plot.
- A legend is included to distinguish between genes and intergenic regions.
- The grid is enabled for better readability.
-
Displaying the Plot:
- The
plt.show() function displays the plot.
Notes:
- Ensure that the file
genes_averages.csv is in the same directory as the script or provide the correct path.
- If there are missing values in the
average column, you may need to handle them before plotting (e.g., by dropping rows with missing values).
- Adjust the
figsize parameter in plt.figure() to change the size of the plot as needed.
This script will generate a clear and visually informative plot based on your data.