2

I would like to draw a time-plot using the matplotlib.dates module. From the tutorial, I know how to set regular interval date like the following:

ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m"))
ax.xaxis.set_minor_...

However, in my data there are some months missing. My xaxis date is more like : Jan, Feb, May, Jul, Sep, Oct, Sep... no pattern is formed...

How can I deal with such situation?.

7
  • Do you want to create a scatter plot, x-axis having month and y axis being values? Commented Nov 2, 2011 at 20:29
  • yes...But the plot is line-plot more likely... Commented Nov 2, 2011 at 22:56
  • how are you data for x axis then? cant you just like create array for time, array for value, and feed like plot(times, values), like that? sorry if not i am not getting your point... Commented Nov 2, 2011 at 23:51
  • Yes, it is also a good solution...But how can I set the x axix into date format? I mean I want the x axis appears like 2009-08, 2010-10 this kind of thing. Thanks in advance. Commented Nov 3, 2011 at 17:06
  • I use ticker to manually set the xaxis.setMajorFormatter(FixedFormatter(dateStr)) into the date format I want. And I use xaxis.set_major_locator(IndexLocator(1, 0)) to set the location of the date str... However, do you have a more decent way to do the same thing? Commented Nov 3, 2011 at 17:37

2 Answers 2

2

If I understand correctly you may need to use

matplotlib.ticker.FixedLocator

Example which puts labels at data points 1, 10 and the last one:

import pylab as plt
from matplotlib.ticker import FixedLocator
from matplotlib.dates import DateFormatter
import numpy as np
from datetime import datetime

# dates (unequally spaced, but works exactly same for equally spaced dates too)
dates = plt.date2num(datetime(2010,3,1))  + np.array([0, 10, 50, 100, 180, 300])
# just some data
data = dates**2
plt.plot_date(dates,data)

ax = plt.gca()
ax.xaxis.set_major_locator(FixedLocator(dates[[1, 3, len(dates)-1]]))
# for some reason this is needed to get the month displayed too
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m"))
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

This does change label format. The only thing I needed was from matplotlib.dates import DateFormatter
Hmm, AnneS's question is a bit unclear. But just using plot_date will put equally spaced ticks and labels on the plot even for unequally spaced data. So, I figured that the question was 'How to set dates labels in x axis with ununiform interval', which above answers.
@yosukesabai Well, as stated in above comment, I don't think DateFormatter is actually needed so I removed it from the code.
0

Starting from Mauro's solution, i tried to plot data pair where dates are irregularly spaced, so that i can find this QA sometime in future.

import pylab as plt
from matplotlib.ticker import FixedLocator
from matplotlib.dates import DateFormatter
import numpy as np
from datetime import datetime

# dates, irregularly spaced
y = 2010, 2010, 2010, 2011, 2011
m =    9,   11,   12,    3,    4


dates = np.array([plt.date2num(datetime(yy,mm,1)) for yy,mm in zip(y,m)])
print dates


# just some data
data = dates**2
print data


plt.plot_date(dates,data)

ax = plt.gca()
# need some more thnking for properly locate which date to label
#ax.xaxis.set_major_locator(FixedLocator(dates[[1, 10, len(dates)-1]]))
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m"))
plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.