Reputation: 7245
I am plotting values over the time (i.e. hourly)
I have a dataframe dfout
as following
val x y ID time
0 6.0 48.730304 11.594837 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:02:02
1 6.5 48.731639 11.602004 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:02:26
2 7.0 48.740104 11.641433 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:04:45
3 6.5 48.744026 11.648048 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:05:11
4 6.0 48.747356 11.654539 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:05:32
5 5.5 48.749050 11.660844 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:05:50
6 5.0 48.749935 11.666812 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:06:10
7 4.5 48.751007 11.677590 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:06:54
8 4.0 48.742317 11.675558 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:08:31
9 4.5 48.736461 11.675782 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:08:55
10 5.0 48.729659 11.675481 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:09:20
11 5.5 48.725871 11.673145 0efd2316-feb9-461c-b102-045aef8e22fe 2014-03-10 08:09:35
and I want to plot the column val
as following
import matplotlib.dates as md
fig=figure(figsize=(20,10))
ax=plt.gca()
xfmt = md.DateFormatter('%H:%M:%S')
plt.xticks( rotation=25 )
ax.xaxis.set_major_formatter(xfmt)
plt.title("Temperature")
plt.ylabel(r'Temperature, $^{o}C$')
ax.plot(datenumes,dfout.val, marker='.',lw=0)
However I would like the xlabel between 08:00:00
to 16:00:00
hour by hour, so: 08:00:00
, 09:00:00
, 10:00:00
,....,16:00:00
.
Upvotes: 3
Views: 1404
Reputation: 3013
Simply choose the position of your ticks using ax.set_xticks()
and feed it a custom list of datetime
objects created for that purpose.
The issue is that the label representations of those object are floats. You can remedy this by creating another list that contains the time-strings as you want them to look like, which I called ticklabels
in this solution.
import matplotlib.dates as md
import matplotlib.pyplot as plt
from datetime import *
val = [6, 6.5, 7, 6.5, 6, 5.5, 5, 4.5, 4, 4.5, 5, 5.5]
datenumes = [datetime(2014, 3, 10, 8, 2, 2),
datetime(2014, 3, 10, 8, 2, 26),
datetime(2014, 3, 10, 10, 4, 45),
datetime(2014, 3, 10, 10, 5, 11),
datetime(2014, 3, 10, 12, 5, 32),
datetime(2014, 3, 10, 12, 5, 50),
datetime(2014, 3, 10, 15, 6, 10),
datetime(2014, 3, 10, 16, 6, 54),
datetime(2014, 3, 10, 17, 8, 31),
datetime(2014, 3, 10, 18, 8, 55),
datetime(2014, 3, 10, 19, 9, 20),
datetime(2014, 3, 10, 22, 9, 35)]
# creates list of datetimes where ticks should be set
ticks = [datetime(2014, 3, 10, 8, 0, 0),
datetime(2014, 3, 10, 10, 0, 0),
datetime(2014, 3, 10, 12, 0, 0),
datetime(2014, 3, 10, 14, 0, 0),
datetime(2014, 3, 10, 16, 0, 0),
datetime(2014, 3, 10, 18, 0, 0),
datetime(2014, 3, 10, 20, 0, 0),
datetime(2014, 3, 10, 22, 0, 0) ]
# generate labels for the datetimes used as ticks
ticklabels = [str(tick)[11:] for tick in ticks]
# PLOT
fig=plt.figure(figsize=(20,10))
ax=plt.gca()
plt.xticks( rotation=25 )
# set the ticks according to the list tick
ax.set_xticks(ticks)
## set labels to human readable format
ax.set_xticklabels(ticklabels)
plt.title("Temperature")
plt.ylabel(r'Temperature, $^{o}C$')
ax.plot(datenumes, val, marker='.', lw=0)
plt.show()
Upvotes: 3