Home  >  Article  >  Backend Development  >  How to Prevent Overlapping Time Stamps on Matplotlib X-Axis?

How to Prevent Overlapping Time Stamps on Matplotlib X-Axis?

DDD
DDDOriginal
2024-11-27 08:29:14232browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn