To add appropriate vertical and horizontal axis marks every million and improve rendering speed, you can make a few adjustments to the plotting code. Here’s the updated code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Load your numpy array (replace 'your_data.npy' with your actual file)
data = np.load('your_data.npy')
# Load the gene ranges from the CSV file
gene_ranges = pd.read_csv('genes.csv', sep=';', header=None)
# Create a mask for the coverage data
coverage_colors = np.full(data.shape, 'blue') # Default color is blue
# Iterate through the gene ranges and set the corresponding colors to red
for index, row in gene_ranges.iterrows():
start = int(row[1]) - 1 # Convert to 0-based index
end = int(row[2]) - 1 # Convert to 0-based index
coverage_colors[start:end + 1] = 'red' # Set the range to red
# Create the bar chart
plt.figure(figsize=(1920, 1080), dpi=300) # Set the figure size for 1080p
# Use a faster rendering method by using the 'bar' function directly
plt.bar(range(len(data)), data, color=coverage_colors, width=1.0)
# Set horizontal axis ticks every million
plt.xticks(ticks=np.arange(0, len(data), 1000000), labels=np.arange(0, len(data), 1000000), rotation=45)
# Set vertical axis ticks
plt.yticks(ticks=np.arange(0, np.max(data) + 1, 1000000), labels=np.arange(0, np.max(data) + 1, 1000000))
# Add grid for better readability
plt.grid(axis='y', linestyle='--', alpha=0.7)
# Turn off the axis for the x-axis
plt.gca().axes.get_xaxis().set_visible(True)
# Save the figure
plt.savefig('flatplot-both.png', bbox_inches='tight')
plt.close()
Key Changes:
- Axis Ticks: The
plt.xticks() and plt.yticks() functions are used to set the ticks on the horizontal and vertical axes every million. The rotation=45 argument is added to make the x-axis labels more readable.
- Grid: A grid is added to the y-axis for better readability, which can help in visualizing the data.
- Rendering Speed: The
width=1.0 parameter in the plt.bar() function can help with rendering speed, especially for large datasets. The figure size and DPI are set in the plt.figure() call to ensure high resolution without unnecessary overhead.
This code will create a bar chart with the specified features and save it as a PNG file. Adjust the tick intervals as needed based on the size of your data.