I am trying get daily data plot. I have 3 months of data and its really hard to point out each day. How can I format x axis, so that I can get every date?
Asked
Active
Viewed 8,873 times
1 Answers
12
Changing the frequency of the major ticks can be done using set_major_locator(mdates.DayLocator(interval=5))
as shown in the example below.
Printing the date for every day for 3 months worth of data results in a very crowded x-axis. So might be worth setting the major ticks like every 5 days and then minor ticks every day, as in the example below.
Also I would suggest having a look at this example from matplotlib and this question which is very similar.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
# Just creating some random data
x = []
day = 1
month = 5
for ix in range(90):
try:
x.append(datetime(2000, month, day))
day +=1
except ValueError:
day = 1
month += 1
y = np.random.uniform(0, 10, len(x))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=5))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.plot(x, y)
fig.autofmt_xdate()
ax.grid(True)
plt.show()
which will give a plot like this (the data will different since its random), minor ticks every day, and major ticks with a label every 5 days.

Trenton McKinney
- 56,955
- 33
- 144
- 158

Hiho
- 643
- 4
- 8