Home > Article > Backend Development > How to Prevent Overlapping Time Stamps on Matplotlib X-Axis?
How to Rotate X-Axis Tick Labels for Non-Overlapping Time Stamps
When dealing with large datasets with time-stamped values, the tick labels on the X-axis can become cluttered and difficult to read. To mitigate this issue, it's desirable to rotate the text labels for improved readability.
To rotate the X-axis tick labels, one can use the plt.xticks(rotation=90) function. This function takes a single argument, which specifies the rotation angle in degrees. A value of 90 degrees rotates the labels vertically, allowing for more legible and non-overlapping labels.
Here's an example where the time stamps are plotted.
import matplotlib.pyplot as plt import datetime # Parse timestamps and delay values 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:]] # Plot the data plt.plot(time, delay) plt.grid(b='on') # Rotate the X-axis tick labels plt.xticks(rotation=90) # Save the plot plt.savefig('test.png')
In this example, the plt.xticks(rotation=90) line ensures that the tick labels on the X-axis are rotated vertically, providing improved readability, particularly when the timestamps are densely packed together.
The above is the detailed content of How to Prevent Overlapping Time Stamps on Matplotlib X-Axis?. For more information, please follow other related articles on the PHP Chinese website!