Home >Backend Development >Python Tutorial >How Can I Improve X-Axis Label Readability in Matplotlib When Plotting Timestamped Data?

How Can I Improve X-Axis Label Readability in Matplotlib When Plotting Timestamped Data?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-28 05:06:09341browse

How Can I Improve X-Axis Label Readability in Matplotlib When Plotting Timestamped Data?

Enhancing X-Axis Label Visibility with Rotation

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!

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