使用'agg'后端

时间:2016-11-02 07:14:46

标签: python-3.x matplotlib

我无法使用'agg'后端生成时间序列图。我的最终目标是生成简单的图表,以后可以将其包含在PDF文档中。我的印象是使用'agg'后端是一个很好的非交互式后端,只需将图表保存为文件即可。我绝对不明白使用不同后端的细微差别,也不确定在哪里查找这些问题。

我首先编写了一个简单的时间序列图表,用于默认的交互式后端,它使用datetime.datetime绘制OrderedDict对象的内容,用于键和值的浮点数。只要我使用默认后端,它就可以正常工作,我认为这是TkAgg。只要我添加一行,matplotlib.use('agg')就会抛出错误并失败。下面的示例代码和堆栈跟踪。

我做错了什么?

代码:没有'agg'后端行

,运行正常
import os
import matplotlib
matplotlib.use('agg') #Remove me and all is well!
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

... define aggData dictionary ...

fig = plt.figure(figsize=(6.5,5))
ax = fig.add_subplot(111)
hfmt = mdates.DateFormatter('%x\n%X')
ax.plot_date([k for k in aggData.keys()], [v for v in aggData.values()], fmt='r-')
ax.xaxis.set_major_formatter(hfmt)
plt.xticks(rotation=60)
plt.subplots_adjust(top=1.0, bottom=0.2)
plt.show()

是的,我知道我必须更改非交互式会话的plt.show(),但由于此脚本在ax.plot_date ...失败,我还没有到达那里。

堆栈追踪:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\...\TestPlot.py", line 260, in demo
    ax.plot_date([k for k in aggData.keys()], [v for v in aggData.values()], fmt='r-')
File "C:\Program Files\Python35\lib\site-packages\matplotlib\__init__.py", line 1819, in inner
    return func(ax, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axes\_axes.py", line 1453, in plot_date
    self.xaxis_date(tz)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axes\_base.py", line 3182, in xaxis_date
    self.xaxis.axis_date(tz)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 1646, in axis_date
    self.update_units(datetime.datetime(2009, 1, 1, 0, 0, 0, 0, tz))
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 1396, in update_units
    self._update_axisinfo()
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 1414, in _update_axisinfo
    self.set_major_locator(info.majloc)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 1515, in set_major_locator
    self.stale = True
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 270, in stale
    self.stale_callback(self, val)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 75, in _stale_axes_callback
    self.axes.stale = val
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 270, in stale
    self.stale_callback(self, val)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\figure.py", line 56, in _stale_figure_callback
    self.figure.stale = val
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 270, in stale
    self.stale_callback(self, val)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\pyplot.py", line 576, in _auto_draw_if_interactive
    fig.canvas.draw_idle()
File "C:\Program Files\Python35\lib\site-packages\matplotlib\backend_bases.py", line 2026, in draw_idle
    self.draw(*args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\backends\backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 62, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\figure.py", line 1159, in draw
    func(*args)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 62, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axes\_base.py", line 2319, in draw
    a.draw(renderer)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\artist.py", line 62, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 1108, in draw
    ticks_to_draw = self._update_ticks(renderer)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 951, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 951, in <listcomp>
    tick_tups = [t for t in self.iter_ticks()]
File "C:\Program Files\Python35\lib\site-packages\matplotlib\axis.py", line 894, in iter_ticks
    majorLocs = self.major.locator()
File "C:\Program Files\Python35\lib\site-packages\matplotlib\dates.py", line 1007, in __call__
    self.refresh()
File "C:\Program Files\Python35\lib\site-packages\matplotlib\dates.py", line 1027, in refresh
    dmin, dmax = self.viewlim_to_dt()
File "C:\Program Files\Python35\lib\site-packages\matplotlib\dates.py", line 771, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\dates.py", line 419, in num2date
    return _from_ordinalf(x, tz)
File "C:\Program Files\Python35\lib\site-packages\matplotlib\dates.py", line 271, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1

0 个答案:

没有答案