绘制值与一天中的时间

时间:2015-11-26 13:38:12

标签: python pandas matplotlib

我正在使用Python和Pandas尝试将值绘制为时间的函数(例如,X轴的范围为00:00:00到23:59:59)。我的输入数据包括完整的时间戳和每个时间戳的相应值。

我可以成功地将值绘制为总时间的函数。但是当我从完整的时间戳中提取时间并尝试绘制时间时,我得到如下所示的错误。

我的方法中缺少什么?这是一些示例代码:

import pandas as pd
import matplotlib.pyplot as plt

my_dates = pd.DatetimeIndex(['2015-01-01 01:23:45', '2015-01-01 12:34:56', '2015-01-02 02:45:00', '2015-01-03 01:22:33'])
my_dates = my_dates.tz_localize('UTC')
values = pd.Series([1, 2, 3, 2.5])
time_of_day = pd.Series(my_dates.time)

df = pd.DataFrame({'vals': values, 'tod' : time_of_day}).set_index(my_dates)
print(df)

输入数据现在如下所示:

                                tod  vals
2015-01-01 01:23:45+00:00  01:23:45   1.0
2015-01-01 12:34:56+00:00  12:34:56   2.0
2015-01-02 02:45:00+00:00  02:45:00   3.0
2015-01-03 01:22:33+00:00  01:22:33   2.5

下面的图表可以使用并生成一个作为总时间函数的图表。

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot_date(x=df.index, y= df.vals, marker='o')

现在我尝试在一天中的时间内绘图并收到错误

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot_date(x=df.tod, y= df.vals, marker='o')

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    335                 pass
    336             else:
--> 337                 return printer(obj)
    338             # Finally look for special method names
    339             method = _safe_get_formatter_method(obj, self.print_method)

C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    205 
    206     if 'png' in formats:
--> 207         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    208     if 'retina' in formats or 'png2x' in formats:
    209         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    115 
    116     bytes_io = BytesIO()
--> 117     fig.canvas.print_figure(bytes_io, **kw)
    118     data = bytes_io.getvalue()
    119     if fmt == 'svg':

C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2156                     orientation=orientation,
   2157                     dryrun=True,
-> 2158                     **kwargs)
   2159                 renderer = self.figure._cachedRenderer
   2160                 bbox_inches = self.figure.get_tightbbox(renderer)

C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    519 
    520     def print_png(self, filename_or_obj, *args, **kwargs):
--> 521         FigureCanvasAgg.draw(self)
    522         renderer = self.get_renderer()
    523         original_dpi = renderer.dpi

C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
    467 
    468         try:
--> 469             self.figure.draw(self.renderer)
    470         finally:
    471             RendererAgg.lock.release()

C:\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

C:\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
   1083         dsu.sort(key=itemgetter(0))
   1084         for zorder, a, func, args in dsu:
-> 1085             func(*args)
   1086 
   1087         renderer.close_group('figure')

C:\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2108 
   2109         for zorder, a in dsu:
-> 2110             a.draw(renderer)
   2111 
   2112         renderer.close_group('axes')

C:\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

C:\Anaconda3\lib\site-packages\matplotlib\axis.py in draw(self, renderer, *args, **kwargs)
   1112         renderer.open_group(__name__)
   1113 
-> 1114         ticks_to_draw = self._update_ticks(renderer)
   1115         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
   1116                                                                 renderer)

C:\Anaconda3\lib\site-packages\matplotlib\axis.py in _update_ticks(self, renderer)
    955 
    956         interval = self.get_view_interval()
--> 957         tick_tups = [t for t in self.iter_ticks()]
    958         if self._smart_bounds:
    959             # handle inverted limits

C:\Anaconda3\lib\site-packages\matplotlib\axis.py in <listcomp>(.0)
    955 
    956         interval = self.get_view_interval()
--> 957         tick_tups = [t for t in self.iter_ticks()]
    958         if self._smart_bounds:
    959             # handle inverted limits

C:\Anaconda3\lib\site-packages\matplotlib\axis.py in iter_ticks(self)
    899         Iterate through all of the major and minor ticks.
    900         """
--> 901         majorLocs = self.major.locator()
    902         majorTicks = self.get_major_ticks(len(majorLocs))
    903         self.major.formatter.set_locs(majorLocs)

C:\Anaconda3\lib\site-packages\matplotlib\dates.py in __call__(self)
    864     def __call__(self):
    865         'Return the locations of the ticks'
--> 866         self.refresh()
    867         return self._locator()
    868 

C:\Anaconda3\lib\site-packages\matplotlib\dates.py in refresh(self)
    881     def refresh(self):
    882         'Refresh internal information based on current limits.'
--> 883         dmin, dmax = self.viewlim_to_dt()
    884         self._locator = self.get_locator(dmin, dmax)
    885 

C:\Anaconda3\lib\site-packages\matplotlib\dates.py in viewlim_to_dt(self)
    625     def viewlim_to_dt(self):
    626         vmin, vmax = self.axis.get_view_interval()
--> 627         return num2date(vmin, self.tz), num2date(vmax, self.tz)
    628 
    629     def _get_unit(self):

C:\Anaconda3\lib\site-packages\matplotlib\dates.py in num2date(x, tz)
    343         tz = _get_rc_timezone()
    344     if not cbook.iterable(x):
--> 345         return _from_ordinalf(x, tz)
    346     else:
    347         x = np.asarray(x)

C:\Anaconda3\lib\site-packages\matplotlib\dates.py in _from_ordinalf(x, tz)
    233     dt = datetime.datetime(
    234         dt.year, dt.month, dt.day, int(hour), int(minute), int(second),
--> 235         microsecond, tzinfo=UTC).astimezone(tz)
    236 
    237     if microsecond > 999990:  # compensate for rounding errors

TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'

乍一看,这似乎是matplotlib \ dates.py本身的调用问题?我使用的是Python 3.5.0,matplotlib 1.4.3和pandas 0.17.0。

如何更改我的方法以获取我正在寻找的图表?谢谢!

0 个答案:

没有答案