3

I have the following dataframe:

Time    test
0:05    2
0:06    2
0:07    2
0:08    2
0:09    2
0:10    2
0:11    2

This datframe starts at 0:00 and ends at 11:59. I created the following graph with:

x = dftestgraph['Time']
y = dftestgraph['test']
plt.ylabel('Number of tasks')
plt.plot(x, y, color="#00a1e4", label="Number of Tasks")
plt.fill(x, y, '#00a1e4', alpha=0.8)
plt.show()

Graph

Why is there a line in the bottom of the graph, that splits my filled graph in half? And I would like to format my x-axis as ( 0:00, 0:30, 1:00 etc.) I tried:

plt.xticks(0:00, 11:59, 30:00))

However this does not work. My question is:

  • Why is there a line in my graph and how can I solve this?
  • How to set the x-axis in the correct format?
F1990
  • 627
  • 2
  • 9
  • 20
  • 1
    Check that the last data point isn't 0:00 and sending your line back to the beginning of your plot. – xnx Jul 29 '15 at 12:37

1 Answers1

7

plt.fill basically links the first and last point of the time series to build its polygon. I would use fill_between instead.

I've put together a MWE below that shows how it can be done. It also show a way to format the xaxis labels as you wanted, derived from the following posts: Creating graph with date and time in axis labels with matplotlib and Plotting time in Python with Matplotlib.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import dates
import datetime

plt.close('all')

#---- generate some data ----

t = [datetime.datetime(2015, 1, 1, 0) + 
     datetime.timedelta(hours=i) for i in np.arange(0,12.1,0.5)]
x = np.random.rand(len(t))

#---- create figure and axe ----

fig = plt.figure()

ax = fig.add_axes([0.1, 0.2, 0.85, 0.75])

#---- format xaxis ----

# position of the labels
xtk_loc = [datetime.datetime(2015, 1, 1, 0) + 
           datetime.timedelta(hours=i) for i in np.arange(0,12.1,0.5)]
ax.set_xticks(xtk_loc)
ax.tick_params(axis='both', direction='out', top='off', right='off')

# format of the labels
hfmt = dates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(hfmt)
fig.autofmt_xdate(rotation=90, ha='center')

#---- set axes labels ----

ax.set_ylabel('Number of tasks', labelpad=10, fontsize=14)
ax.set_xlabel('Time', labelpad=20, fontsize=14)

#---- plot data ----

ax.plot(t, x, color="#004c99", label="Number of Tasks")
ax.fill_between(t, x, 0, facecolor='#00a1e4', alpha=0.5, lw=0.5)

#---- set axis limits ----

timemin = datetime.datetime(2015, 1, 1, 0)
timemax = datetime.datetime(2015, 1, 1, 12)

ax.axis(xmin=timemin, xmax=timemax)

plt.show()  

which results in:

enter image description here

Community
  • 1
  • 1
Jean-Sébastien
  • 2,649
  • 1
  • 16
  • 21
  • great explanation and help! Thank's! – F1990 Jul 29 '15 at 15:03
  • @F1990 Cool. I've updated my example so that now the axis limits are also controlled. The time now run all the way up to 12:00, and not just 11:30. – Jean-Sébastien Jul 29 '15 at 15:06
  • Great, I am going to implement it in my own code. Maybe you know also a good solution to my other question? http://stackoverflow.com/questions/31694538/count-if-job-is-in-a-certain-time-interval – F1990 Jul 29 '15 at 15:07