If you'd like to remove the gene names below the bars and instead just show numerical indices (or no labels at all), we can modify the script accordingly. Below is the updated Python script where the gene names are replaced with numerical indices on the x-axis:
import pandas as pd
import matplotlib.pyplot as plt
# Read the CSV file
df = pd.read_csv('genes_averages.csv', sep=';')
# Create lists for genes and intergenic regions
gene_indices = []
intergenic_indices = []
# Separate indices for genes and intergenic regions
for index, row in df.iterrows():
if 'INTERGENIC' in row['Gene name']:
intergenic_indices.append(index)
else:
gene_indices.append(index)
# Create figure and axis
plt.figure(figsize=(12, 6))
# Plot bars
x = range(len(df))
bars = plt.bar(x, df['average'], width=0.8)
# Color the bars based on whether they're genes or intergenic regions
for i, bar in enumerate(bars):
if 'INTERGENIC' in df.iloc[i]['Gene name']:
bar.set_color('blue')
else:
bar.set_color('red')
# Customize the plot
plt.xlabel('Index')
plt.ylabel('Average')
plt.title('Gene and Intergenic Region Averages')
# Use numerical indices for the x-axis
plt.xticks(x, [str(i) for i in x], rotation=0)
# Add legend
from matplotlib.patches import Patch
legend_elements = [Patch(facecolor='red', label='Genes'),
Patch(facecolor='blue', label='Intergenic Regions')]
plt.legend(handles=legend_elements)
# Adjust layout to prevent label cutoff
plt.tight_layout()
# Show the plot
plt.show()
Key Changes:
-
Removed Gene Names from X-Axis:
- Instead of using
df['Gene name'] for the x-axis labels, I replaced it with numerical indices ([str(i) for i in x]).
- This ensures that the x-axis simply shows the position of each bar (e.g.,
0, 1, 2, ...) rather than the gene names.
-
Simplified X-Axis Ticks:
- The
rotation=0 ensures that the numerical indices are displayed horizontally.
-
Legend Still Present:
- The legend remains to differentiate between genes (red) and intergenic regions (blue).
Output:
- The x-axis will now display numerical indices corresponding to the rows in your CSV file.
- The y-axis will still represent the
average values.
- Bars will be colored red for genes and blue for intergenic regions.
Let me know if you need further adjustments!