gpt4 book ai didi

python - matplotlib - 直接在 Canvas 上绘图

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:32 24 4
gpt4 key购买 nike

由于动态更新的性能问题,我需要在 Canvas 上直接绘制很多矩形作为非常低的级别,也就是说不使用 matplotlib.patches 而我们必须这样做具有经典的 GUI。

更准确地说,我只想画一个矩形,而不是所有图形。

这可能吗?

这是我使用 Joe Kington 提供的链接的测试代码。

#!/usr/bin/env python3

from random import randint, choice
import time

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.colors as colors
import matplotlib

back_color = "black"
colors = ['red', 'green', 'cyan', 'yellow']

width = 16
height = 16

ax = plt.subplot(111)
canvas = ax.figure.canvas

ax.set_xlim([0, width])
ax.set_ylim([0, height])

def update():
global ax, canvas, colors, width, height

x = randint(0, width - 1)
y = randint(0, height - 1)

rect = mpatches.Rectangle(
(x, y), 1, 1,
facecolor = choice(colors),
edgecolor = back_color
)

start = time.time()
ax.draw_artist(rect)
canvas.blit(ax.bbox)
print("draw >>>", time.time() - start)

timer = canvas.new_timer(interval = 1)
timer.add_callback(update)
timer.start()

plt.show()

在 Mac O$ 下,我收到以下消息。

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backend_bases.py", line 1203, in _on_timer
ret = func(*args, **kwargs)
File "/Users/xxx/test.py", line 86, in update
ax.draw_artist(rect)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/axes.py", line 2100, in draw_artist
a.draw(self._cachedRenderer)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/artist.py", line 56, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/patches.py", line 393, in draw
gc = renderer.new_gc()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backends/backend_macosx.py", line 97, in new_gc
self.gc.save()
RuntimeError: CGContextRef is NULL

对于特立独行的用户

对于想要使用以下代码以及动画的 Mac 用户,我有一个好消息。我在没有任何软件的情况下在我的 Mac 上重新安装了 Maverick OS,这称为全新安装,然后我安装了 Anaconda 和 XQuark(see this)。

要使动画工作,您只需在任何其他 matplotlib 导入之前使用以下两行。

import matplotlib
matplotlib.use('TkAgg')

我认为这适用于 Anaconda 支持的任何 Mac 操作系统。只有 Maverick 才需要使用 XQuark。

最佳答案

您当前的代码有两个问题。

  1. 您正在尝试在没有先绘制 Canvas 的情况下进行 blit。您收到的错误是特定于后端的,但基本上是说 Canvas 的渲染器尚未初始化。
  2. 您没有将矩形艺术家添加到坐标区。因此,您调用 ax.draw_artist(rect) 时不会绘制它。

这是一个工作示例:

from random import randint, choice
import time
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

back_color = "black"
colors = ['red', 'green', 'cyan', 'yellow']
width, height = 16, 16

fig, ax = plt.subplots()
ax.set(xlim=[0, width], ylim=[0, height]) # Or use "ax.axis([x0,x1,y0,y1])"

# Be sure to draw the canvas once before we start blitting. Otherwise
# a) the renderer doesn't exist yet, and b) there's noting to blit onto
fig.canvas.draw()

def update():
x = randint(0, width - 1)
y = randint(0, height - 1)

rect = mpatches.Rectangle(
(x, y), 1, 1,
facecolor = choice(colors),
edgecolor = back_color
)
ax.add_artist(rect)

start = time.time()
ax.draw_artist(rect)
fig.canvas.blit(ax.bbox)
print("draw >>>", time.time() - start)

timer = fig.canvas.new_timer(interval=1)
timer.add_callback(update)
timer.start()

plt.show()

不过,在这一点上,不每次都添加新艺术家会更有意义。相反,添加初始矩形并每次更新它。例如:

from random import randint, choice
import time
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

back_color = "black"
colors = ['red', 'green', 'cyan', 'yellow']

width, height = 16, 16

fig, ax = plt.subplots()
ax.set(xlim=[0, width], ylim=[0, height]) # Or use "ax.axis([x0,x1,y0,y1])"

rect = mpatches.Rectangle(
(0, 0), 1, 1,
facecolor = choice(colors),
edgecolor = back_color
)
ax.add_artist(rect)

# Be sure to draw the canvas once before we start blitting. Otherwise
# a) the renderer doesn't exist yet, and b) there's noting to blit onto
fig.canvas.draw()

def update():
x = randint(0, width - 1)
y = randint(0, height - 1)
rect.set(xy=[x,y], facecolor=choice(colors))

start = time.time()
ax.draw_artist(rect)
fig.canvas.blit(ax.bbox)
print("draw >>>", time.time() - start)

timer = fig.canvas.new_timer(interval=1)
timer.add_callback(update)
timer.start()

plt.show()

关于python - matplotlib - 直接在 Canvas 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22376437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com