Matplotlib draw_artist(axis.patch) 在 MacOSX 后端上失败
Matplotlib draw_artist(axis.patch) fails on MacOSX backend
我正在关注 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)
此代码在 Ubuntu 14.04 和 Python 3.4 + Qt5Agg 后端上运行良好。但是在 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
如有任何建议,我们将不胜感激!
您需要使用不同的后端。
将这些行添加到脚本的开头:
import matplotlib
matplotlib.use('Qt4Agg')
这给了我 283 个地块,而 fig.canvas.draw()
只给了 26 个地块。
我正在关注 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)
此代码在 Ubuntu 14.04 和 Python 3.4 + Qt5Agg 后端上运行良好。但是在 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
如有任何建议,我们将不胜感激!
您需要使用不同的后端。 将这些行添加到脚本的开头:
import matplotlib
matplotlib.use('Qt4Agg')
这给了我 283 个地块,而 fig.canvas.draw()
只给了 26 个地块。