When visualizing timestamped data, it can be challenging to read overlapping labels on the x-axis as the number of samples increases. To address this issue, rotating the text on the x-axis can improve legibility. Let's explore how to achieve this using Matplotlib.
Implementation:
import matplotlib.pyplot as plt # Load data from CSV file values = open('stats.csv', 'r').readlines() time = [datetime.datetime.fromtimestamp(float(i.split(',')[0].strip())) for i in values[1:]] delay = [float(i.split(',')[1].strip()) for i in values[1:]] plt.plot(time, delay) plt.grid(b='on') # Rotate x-axis labels plt.xticks(rotation=90) plt.savefig('test.png')
By adding the line plt.xticks(rotation=90), we rotate the x-axis tick labels 90 degrees clockwise. This effectively stacks the labels vertically, improving their readability even when they overlap.
The above is the detailed content of How Can I Improve X-Axis Label Readability in Matplotlib When Plotting Timestamped Data?. For more information, please follow other related articles on the PHP Chinese website!