【发布时间】:2016-08-10 20:45:07
【问题描述】:
我正在关注Speeding up Matplotlib -- Bastibe,通过仅更新图中已更改的内容来测试制作动画。我正在使用 MacPorts 安装的 Mac OS X 10.11.4、Python 3.4。代码如下:
'''Import modules'''
import matplotlib.pyplot as plt
import numpy as np
import time
'''Initialize figure and axis, perform first draw on canvas'''
fig, ax = plt.subplots()
line, = ax.plot(np.random.randn(100))
plt.show(block=False)
fig.canvas.draw()
'''Count how many plots made within 1 second'''
tstart = time.time()
num_plots = 0
while time.time()-tstart < 1: # within 1 second
line.set_ydata(np.random.randn(100)) # update line
ax.draw_artist(ax.patch) # draw background
ax.draw_artist(line) # draw line
fig.canvas.update() # update canvas
fig.canvas.flush_events()
num_plots += 1 # count++
print(num_plots)
此代码在带有 Python 3.4 + Qt5Agg 后端的 Ubuntu 14.04 上运行良好。但在 Mac 上,它会报告
Traceback (most recent call last):
File "./test.py", line 19, in <module>
ax.draw_artist(ax.patch)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_base.py", line 2340, in draw_artist
a.draw(self._cachedRenderer)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/patches.py", line 486, in draw
gc = renderer.new_gc()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/backends/backend_macosx.py", line 99, in new_gc
self.gc.save()
RuntimeError: CGContextRef is NULL
欢迎提出任何建议!
【问题讨论】:
标签: macos python-3.x matplotlib runtime-error