24

我有两个 numpy 数组 1D,一个是 datetime64 格式的测量时间,例如:

array([2011-11-15 01:08:11, 2011-11-16 02:08:04, ..., 2012-07-07 11:08:00], dtype=datetime64[us])

和其他具有整数数据的相同长度和维度的数组。
我想在 matplotlib 时间与数据中绘制一个图。如果我直接放入数据,这就是我得到的:

plot(timeSeries, data)

在此处输入图像描述

有没有办法在更自然的单位中获得时间?例如,在这种情况下,月/年就可以了。

编辑:
我已经尝试过 Gustav Larsson 的建议,但是出现错误:

Out[128]:
[<matplotlib.lines.Line2D at 0x419aad0>]
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/zmq/pylab/backend_inline.pyc in show(close)
    100     try:
    101         for figure_manager in Gcf.get_all_fig_managers():
--> 102             send_figure(figure_manager.canvas.figure)
    103     finally:
    104         show._to_draw = []

/usr/lib/python2.7/dist-packages/IPython/zmq/pylab/backend_inline.pyc in send_figure(fig)
    209     """
    210     fmt = InlineBackend.instance().figure_format
--> 211     data = print_figure(fig, fmt)
    212     # print_figure will return None if there's nothing to draw:
    213     if data is None:

/usr/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt)
    102     try:
    103         bytes_io = BytesIO()
--> 104         fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight')
    105         data = bytes_io.getvalue()
    106     finally:

/usr/lib/pymodules/python2.7/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   1981                     orientation=orientation,
   1982                     dryrun=True,
-> 1983                     **kwargs)
   1984                 renderer = self.figure._cachedRenderer
   1985                 bbox_inches = self.figure.get_tightbbox(renderer)

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    467 
    468     def print_png(self, filename_or_obj, *args, **kwargs):
--> 469         FigureCanvasAgg.draw(self)
    470         renderer = self.get_renderer()
    471         original_dpi = renderer.dpi

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.pyc in draw(self)
    419 
    420         try:
--> 421             self.figure.draw(self.renderer)
    422         finally:
    423             RendererAgg.lock.release()

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/figure.pyc in draw(self, renderer)
    896         dsu.sort(key=itemgetter(0))
    897         for zorder, a, func, args in dsu:
--> 898             func(*args)
    899 
    900         renderer.close_group('figure')

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in draw(self, renderer, inframe)
   1995 
   1996         for zorder, a in dsu:
-> 1997             a.draw(renderer)
   1998 
   1999         renderer.close_group('axes')

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)
   1039         renderer.open_group(__name__)
   1040 
-> 1041         ticks_to_draw = self._update_ticks(renderer)
   1042         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
   1043 

/usr/lib/pymodules/python2.7/matplotlib/axis.pyc in _update_ticks(self, renderer)
    929 
    930         interval = self.get_view_interval()
--> 931         tick_tups = [ t for t in self.iter_ticks()]
    932         if self._smart_bounds:
    933             # handle inverted limits

/usr/lib/pymodules/python2.7/matplotlib/axis.pyc in iter_ticks(self)
    876         Iterate through all of the major and minor ticks.
    877         """
--> 878         majorLocs = self.major.locator()
    879         majorTicks = self.get_major_ticks(len(majorLocs))
    880         self.major.formatter.set_locs(majorLocs)

/usr/lib/pymodules/python2.7/matplotlib/dates.pyc in __call__(self)
    747     def __call__(self):
    748         'Return the locations of the ticks'
--> 749         self.refresh()
    750         return self._locator()
    751 

/usr/lib/pymodules/python2.7/matplotlib/dates.pyc in refresh(self)
    756     def refresh(self):
    757         'Refresh internal information based on current limits.'
--> 758         dmin, dmax = self.viewlim_to_dt()
    759         self._locator = self.get_locator(dmin, dmax)
    760 

/usr/lib/pymodules/python2.7/matplotlib/dates.pyc in viewlim_to_dt(self)
    528     def viewlim_to_dt(self):
    529         vmin, vmax = self.axis.get_view_interval()
--> 530         return num2date(vmin, self.tz), num2date(vmax, self.tz)
    531 
    532     def _get_unit(self):

/usr/lib/pymodules/python2.7/matplotlib/dates.pyc in num2date(x, tz)
    287     """
    288     if tz is None: tz = _get_rc_timezone()
--> 289     if not cbook.iterable(x): return _from_ordinalf(x, tz)
    290     else: return [_from_ordinalf(val, tz) for val in x]
    291 

/usr/lib/pymodules/python2.7/matplotlib/dates.pyc in _from_ordinalf(x, tz)
    201     if tz is None: tz = _get_rc_timezone()
    202     ix = int(x)
--> 203     dt = datetime.datetime.fromordinal(ix)
    204     remainder = float(x) - ix
    205     hour, remainder = divmod(24*remainder, 1)

OverflowError: signed integer is greater than maximum

这可能是一个错误吗?或者我错过了什么。我还尝试了一些简单的方法:

import matplotlib.pyplot as plt
import numpy as np
dates=np.array(["2011-11-13", "2011-11-14", "2011-11-15", "2011-11-16", "2011-11-19"], dtype='datetime64[us]')
data=np.array([1, 2, 3, 4, 5])
plt.plot_date(dates, data)
plt.show()

我仍然收到此错误:

OverflowError: signed integer is greater than maximum

我不明白我在做什么错。ipython 0.13,matplotlib 1.1,Ubuntu 12.04 x64。

最终编辑: matplotlib
似乎不支持dtype=datetime64,所以我需要将.timeSeriesdatetime.datetimedatetime

4

4 回答 4

22
from datetime import datetime
a=np.datetime64('2002-06-28').astype(datetime)
plot_date(a,2)
于 2013-06-20T09:36:28.723 回答
3

你可能想试试这个:

plot_date(timeSeries, data)

默认情况下,x 轴将被视为日期轴,并且是常规轴。这可以定制。

于 2012-07-07T15:10:29.747 回答
3

Matplotlib>=2.2 原生支持绘制 datetime64 数组。请参阅https://github.com/matplotlib/matplotlib/blob/master/doc/users/prev_whats_new/whats_new_2.2.rst#support-for-numpydatetime64

Matplotlib 在 matplotlib.dates 中长期支持 datetime.datetime 日期。我们现在也支持 numpy.datetime64 日期。任何可以使用 datetime.datetime 的地方,都可以使用 numpy.datetime64。例如:

time = np.arange('2005-02-01', '2005-02-02', dtype='datetime64[h]')
plt.plot(time)
于 2019-02-21T11:38:17.233 回答
0

我有一个类似的问题。有时,日期轴正确地绘制了我的 np.datetim64 数组,而在其他时候它没有使用相同的时间数组,而是在日期轴上给出了一些不可识别的整数值。

原因是我在第一次使用对数刻度后设置了 ax.xscale('linear') 。删除 ax.xscale('linear') 解决了这个问题。我了解到,线性轴不是日期时间轴。

于 2021-04-14T18:57:53.957 回答