1

Hey I spend a good amount at google but couldn't find a solution for this:

I'm plotting a graph embededded in a pygame window using a non-gui-backend:

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))

Now I want to create a new window plotting different data. I always get the Error:

UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure

I tried it with this Post which leads to this:

RuntimeError: main thread is not in main loop

So ist there any way to use both?

AUBSieGUL
  • 474
  • 1
  • 5
  • 17
  • 1
    This is not really clear. Why don't you use a GUI backend from the beginning, like `mpl.use("TkAgg")`? What exactly is `pyplot` needed for in this case? – ImportanceOfBeingErnest Mar 13 '18 at 12:02
  • Sorry for the misunderstanding. I need a pyplot graph embedded in my pygamewindow therefore I use the non-GUI backend. Now I need in addition to the embedded graph a new graph in a seperate interactive GUI. Therefore the question if it is possible to use two backends in one programm. – AUBSieGUL Mar 13 '18 at 19:52
  • This does not answer the question. What happens if you use `mpl.use("TkAgg")`? – ImportanceOfBeingErnest Mar 13 '18 at 20:00
  • The same as for switching. The RuntimeError: main thread is not in main loop occures. – AUBSieGUL Mar 13 '18 at 20:13
  • I wonder where the main loop comes from. I do not have pygame available, so I cannot test anything. Where exactly are you running this from? Did you try my other suggestion, not to use pyplot at all? – ImportanceOfBeingErnest Mar 13 '18 at 20:23
  • I use a thread to stop windows from killing pygame when calculating and not responding immediately . No, I will try that now. I just thought der may be an easer way to do this. Thanks for the help. – AUBSieGUL Mar 13 '18 at 20:39
  • You cannot use two backends simultaneously, but you may switch backends. However this seems to fail for you. Note that you do not actually show the code where switching fails, so it's impossible to say anything about that. On the other hand, if `mpl.use("TkAgg")` fails, it suggests that the problem is already earlier. Hence my suggestion to not use pyplot, but completely rely on `Figure` and `FigureCanvasAgg`. – ImportanceOfBeingErnest Mar 13 '18 at 20:43

0 Answers0