0

I'm having a little trouble trying to place evenly spaced ticks in the x_axis using the plot_date of matplotlib. Let's say I have some data y, (some random data in this case), that are temporally spaced and I would like to add some ticks to the x axis so that the viewer can resolve the corresponding months/weeks and get better insight into the data.

Any idea how this is possible ?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker

time1 = '2014-08-01'
time2 = '2016-08-31'

start = mdates.strpdate2num('%Y-%m-%d')(time1)
end = mdates.strpdate2num('%Y-%m-%d')(time2)

n = 1000
step = (end-start)/n

x = [x*step +start for x in range(n)]
y = np.random.randint(0,100,n) /100.


plt.rc('text', usetex=True)
plt.rc('font', family='Times',size=20)
fig = plt.figure(figsize=(18, 12))


plt.plot_date(x, y)
plt.savefig('test.png',bbox_inches='tight', format='png')

enter image description here

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
mysterium
  • 143
  • 1
  • 8
  • 1
    Do you want to use a [MonthLocator](https://matplotlib.org/api/dates_api.html#matplotlib.dates.MonthLocator) or [DayLocator](https://matplotlib.org/api/dates_api.html#matplotlib.dates.DayLocator). What would be the problem of using them? – ImportanceOfBeingErnest Jan 31 '18 at 17:43
  • Thank you, I was not familiar with MonthLocator and it solved the issue. – mysterium Feb 01 '18 at 13:57

0 Answers0