Roy Smith
Roy Smith

Reputation: 2159

Specify tick format for plot_date()?

I'm using:

matplotlib '1.1.1rc'
ipython 0.12.1
ipython notebook --pylab inline --no-browser

I generate a graph with:

xticks(rotation='vertical')
plot_date(ts, percent, xdate=True)

where ts is a list of datetimes spanning several days. I get:

enter image description here

Note the x-axis; it's only showing the time portion of the datetime. How do I get it to also show the date?

Upvotes: 2

Views: 5231

Answers (2)

fantabolous
fantabolous

Reputation: 22726

Pandas, which is great for time series and data analysis in general, has a wrapper around matplotlib that takes care of most of this for you. In your example you could probably just do:

import pandas as pd
df = pd.DataFrame(ts)  # to get your series into a pandas dataframe
df.plot()
plot.show()

It will gracefully handle the date formatting and whatnot.
See http://pandas.pydata.org/pandas-docs/stable/visualization.html

Upvotes: 1

Greg
Greg

Reputation: 12234

If you take a look at the standard example for matplotlibs datetime if you are following a similar idea then you can instruct the plotting tool how to format the ticks in the line:

from matplotlib.dates import  DateFormatter
ax.xaxis.set_major_formatter( DateFormatter('%Y-%m-%d') )

Changing the string to

"%Y-%m-%d %H:%M:%S" 

will had the time to the x ticks in their example.

Upvotes: 5

Related Questions