Reputation: 31662
I would like to assign for x axis in matplotlib plot full date with time but with autoscale I could get only times or dates but not both.
import matplotlib.pyplot as plt
import pandas as pd
times = pd.date_range('2015-10-06', periods=500, freq='10min')
fig, ax = plt.subplots(1)
fig.autofmt_xdate()
plt.plot(times, range(times.size))
plt.show()
And on x axis I get only times without any dates so it's hard to distinct measurements.
I think that it's some option in matplotlib in matplotlib.dates.AutoDateFormatter but I couldn't find any one that could allow me to change that autoscale.
Upvotes: 18
Views: 56318
Reputation: 599
This is how to plot date and time column when it is not your index column.
fig, ax = plt.subplots() ax.plot('docdate', 'count', data=newdf) fig.autofmt_xdate() plt.show()
Upvotes: 0
Reputation: 3828
plt.figure()
plt.plot(...)
plt.gcf().autofmt_xdate() plt.show()
Upvotes: 1
Reputation: 69076
You can do this with a matplotlib.dates.DateFormatter
, which takes a strftime
format string as its argument. To get a day-month-year hour:minute
format, you can use %d-%m-%y %H:%M
:
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
times = pd.date_range('2015-10-06', periods=500, freq='10min')
fig, ax = plt.subplots(1)
fig.autofmt_xdate()
plt.plot(times, range(times.size))
xfmt = mdates.DateFormatter('%d-%m-%y %H:%M')
ax.xaxis.set_major_formatter(xfmt)
plt.show()
Upvotes: 34