Reputation: 309
I am trying to plot a series of sunset times in matplotlib but I get the following error: "TypeError: Empty 'DataFrame': no numeric data to plot"
I have looked at several options to convert, e.g. plt.dates.date2num but that doesn't really fullfil my needs as i would like to plot it in a readable format, i.e. times. All examples I have found have times on the x-axis but non have them on the y-axis.
Is there no way of accomplishing this task? Has anyone got an idea?
I am looking very forward to your replies. Best regards, Arne
3 Jan 2013 16:44:00
4 Jan 2013 16:45:00
5 Jan 2013 16:46:00
6 Jan 2013 16:47:00
7 Jan 2013 16:48:00
8 Jan 2013 16:49:00
9 Jan 2013 16:51:00
10 Jan 2013 16:52:00
11 Jan 2013 16:53:00
12 Jan 2013 16:55:00
13 Jan 2013 16:56:00
14 Jan 2013 16:57:00
Upvotes: 7
Views: 7778
Reputation: 284910
It's not quite clear from your question if you're trying to plot some unspecified data on the x-axis with date/time on the y-axis or if you're trying to plot days on the x-axis with times on the y-axis.
From your question, though, I'm going to assume it's the latter.
It sounds like you might be using pandas
, but for the moment, I'll just assume you have two sequences of strings: One with the day, and another sequence with the time.
To treat a given axis as dates, just call ax.xaxis_date()
or ax.yaxis_date()
. In this case, both will actually be dates. (The times will have today as the day, though you won't see this directly.)
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
date = ['3 Jan 2013', '4 Jan 2013', '5 Jan 2013', '6 Jan 2013', '7 Jan 2013',
'8 Jan 2013', '9 Jan 2013', '10 Jan 2013', '11 Jan 2013', '12 Jan 2013',
'13 Jan 2013', '14 Jan 2013']
time = ['16:44:00', '16:45:00', '16:46:00', '16:47:00', '16:48:00', '16:49:00',
'16:51:00', '16:52:00', '16:53:00', '16:55:00', '16:56:00', '16:57:00']
# Convert to matplotlib's internal date format.
x = mdates.datestr2num(date)
y = mdates.datestr2num(time)
fig, ax = plt.subplots()
ax.plot(x, y, 'ro-')
ax.yaxis_date()
ax.xaxis_date()
# Optional. Just rotates x-ticklabels in this case.
fig.autofmt_xdate()
plt.show()
Upvotes: 7