user3600497
user3600497

Reputation: 1661

Plotting dates with plot_date

I have a bunch of dates in the form MM / DD / YYYY. I use a parser to get them into something that looks like datetime.datetime(YYYY, MM, DD, 0, 0) .

I have data corresponding to each date in an array y and would like to plot the two arrays against one another. Using matplotlib.dates.date2num I plot them as

x= matplotlib.dates.date2num(dates)

plot_date(dates,y)

When I do this, I get the following plot

enter image description here

Where as I would prefer something that looks like a time series.

How can I fix this

Upvotes: 1

Views: 406

Answers (1)

Deditos
Deditos

Reputation: 1415

Presumably the dates and data that you're reading in are not in time order, in which case you'll need to sort them both before they're passed to plot_date():

s = np.argsort(dates)
plot_date(dates[s],y[s])

This should work whether you sort on the list/array of datetime instances (your dates) or their numerical equivalents (your x).

Upvotes: 2

Related Questions