Python Matplotlib:如何在一个程序中使用非gui和gui后端?

时间:2018-03-13 02:31:19

标签: python python-3.x matplotlib

嘿,我在谷歌花了不少钱,但无法找到解决方案:

我正在使用非gui-backend绘制在pygame窗口中嵌入的图形:

import matplotlib as mpl
mpl.use("Agg")

import matplotlib.backends.backend_agg as agg
import matplotlib.pyplot as plt

fig = plt.figure(num=3, figsize=[3.5, 2.6], # Inches
                dpi=100,        # 100 dots per inch, so the resulting buffer is 400x400 pixels
                )
x = [1,2,3]
y = [1,2,3]

ax = fig.gca()
plt.step(x, y, 'r')

canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()

screen = pygame.display.get_surface()

size = canvas.get_width_height()

surf = pygame.image.fromstring(raw_data, size, "RGB")
screen.blit(surf, (0,pictureSize_y))

现在我想创建一个绘制不同数据的新窗口。我总是得到错误:

  

UserWarning:matplotlib目前正在使用非GUI后端,因此无法显示数字

我尝试使用this Post导致了这一点:

  

RuntimeError:主线程不在主循环中

那么有没有办法同时使用它们?

0 个答案:

没有答案
相关问题