Matplotlib - 漂浮到HH:MM

时间:2013-11-05 14:30:03

标签: python time matplotlib format python-3.3

所有晚上, 我正在努力生产yaxis为HH:MM。我认为使用下面的两个#'d行会做到这一点,但是它们会产生错误,可以在下面看到。非常感谢任何帮助。

x = ['23/09/2013', '24/09/2013', '25/09/2013', '26/09/2013', '27/09/2013']
ytemp = ['03:04:54', '03:26:29', '03:41:13', '03:20:07', '01:01:04']

import datetime
from matplotlib.dates import date2num, MINUTES_PER_DAY, SEC_PER_DAY, DateFormatter
import pylab as p

# function:  Represent a string time in seconds.
def convert(s):
    h,m,s = map(float, s.split(':'))
    return h/24. + m/MINUTES_PER_DAY + s/SEC_PER_DAY

for i in ytemp:
    y.append(convert(str(i)))

fig = p.figure()
ax = fig.add_subplot(111)
N = len(y)
ind = range(N)
ax.bar(ind, y, facecolor='#777777', align='center')
#ax.yaxis_date()
#ax.yaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.set_ylabel('Idle Time')
ax.set_title(totalidle[1][0],fontstyle='italic')
ax.set_xticks(ind)
ax.set_xticklabels([x[0], x[1], x[2], x[3], x[4]])
fig.autofmt_xdate()
p.show()

y是[0.12840277777777775,0.1433912037037037,0.15362268518518518,0.13896990740740742,0.0424074074074074]

错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 276, in resize
    self.show()
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Pytho    Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 276, in resize
    self.show()
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "C:\Python33\lib\site-packages\matplotlib\artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\figure.py", line 1035, in draw
    func(*args)
  File "C:\Python33\lib\site-packages\matplotlib\artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\axes.py", line 2088, in draw
    a.draw(renderer)
  File "C:\Python33\lib\site-packages\matplotlib\artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 1092, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 946, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 946, in <listcomp>
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 890, in iter_ticks
    majorLocs = self.major.locator()
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 802, in __call__
    self.refresh()
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 819, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 564, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 311, in num2date
    return _from_ordinalf(x, tz)
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 214, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1n33\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "C:\Python33\lib\site-packages\matplotlib\artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\figure.py", line 1035, in draw
    func(*args)
  File "C:\Python33\lib\site-packages\matplotlib\artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\axes.py", line 2088, in draw
    a.draw(renderer)
  File "C:\Python33\lib\site-packages\matplotlib\artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 1092, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 946, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 946, in <listcomp>
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Python33\lib\site-packages\matplotlib\axis.py", line 890, in iter_ticks
    majorLocs = self.major.locator()
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 802, in __call__
    self.refresh()
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 819, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 564, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 311, in num2date
    return _from_ordinalf(x, tz)
  File "C:\Python33\lib\site-packages\matplotlib\dates.py", line 214, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1

1 个答案:

答案 0 :(得分:0)

通过实施:

解决了序数错误
ax.set_ylim(bottom = 733681, top = 733682)

然后#'d out行正常工作。