Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes on example code #3

Closed
AgainPsychoX opened this issue Jan 28, 2024 · 4 comments
Closed

Crashes on example code #3

AgainPsychoX opened this issue Jan 28, 2024 · 4 comments

Comments

@AgainPsychoX
Copy link

My code:

from time import sleep
import pygame
import sys
import math
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('module://pygame_matplotlib.backend_pygame')

fig, ax = plt.subplots()  # Create a figure containing a single axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])  # Plot some data on the axes.
plt.show()

Log:

ygame-ce 2.4.0 (SDL 2.28.5, Python 3.10.11)
Traceback (most recent call last):
  File "D:\Projects\+school\SR\projekt\main.py", line 19, in <module>
    plt.show()
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\pyplot.py", line 527, in show
    return _get_backend_mod().show(*args, **kwargs)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\pygame_matplotlib\backend_pygame.py", line 369, in show
    manager.show(block)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\pygame_matplotlib\backend_pygame.py", line 545, in show
    self.canvas.draw()
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\pygame_matplotlib\backend_pygame.py", line 462, in draw
    self.figure.draw(self.renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\artist.py", line 39, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\pygame_matplotlib\backend_pygame.py", line 68, in draw
    return super().draw(renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\figure.py", line 3154, in draw
    mimage._draw_list_compositing_images(
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\axes\_base.py", line 3070, in draw
    mimage._draw_list_compositing_images(
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\patches.py", line 588, in draw
    self._draw_paths_with_artist_properties(
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\matplotlib\patches.py", line 573, in _draw_paths_with_artist_properties
    renderer.draw_path(gc, *draw_path_args)
  File "D:\Projects\+school\SR\projekt\venv\lib\site-packages\pygame_matplotlib\backend_pygame.py", line 176, in draw_path
    draw_func(
TypeError: function takes at most 4 arguments (5 given)

Other example (using fig.canvas.draw()) also crashes with very similar result.

@lionel42
Copy link
Owner

@AgainPsychoX Hi thanks for trying the package.

I guess you installed with pip. The version published was an old one. This is my fault.

Can you try to upgrade the package ?

pip install --upgrade pygame-matplotlib

@eleweek
Copy link

eleweek commented Mar 17, 2024

This seems to work now.

For other people: if you find this code not working with the correct version, try recreating your virtualenv and reinstalling packages from requirements.txt. For some inexplicable reason, I had to do this and it helped.

@AgainPsychoX
Copy link
Author

I confirm it works. Sorry for late response, I wasn't much interested in my project for a while, but today I wanted to check out few my old projects and decided to revisit this thing.

In my code currently I have something like:

if USE_PYGAME_MATPLOTLIB_BACKEND:
    matplotlib.use('module://pygame_matplotlib.backend_pygame')
else:
    matplotlib.use('Agg')

and in the loop:

        if USE_PYGAME_MATPLOTLIB_BACKEND:
            fig.canvas.draw()
            surf = fig
        else: # `matplotlib.backends.backend_agg` based solution
            canvas = agg.FigureCanvasAgg(fig)
            buffer, w_h = canvas.print_to_buffer()
            surf = pygame.image.frombuffer(buffer, w_h, "RGBA")

The fig is heavy figure with multiple sub-figures, updated between drawing.

There still are some issues with the library:

  • No performance improvement
  • Invalid scaling for some elements (DPI related?)
  • No text rotation
  • No transparency

Using matplotlib.backends.backend_agg:
image

Using pygame-matplotlib:
image

The game state on above screenshots is a bit different as I needed to restart the app to change the values.

Here is link to full source code if you are interested: https://github.com/AgainPsychoX/FuzzyLogicRacingGame

@lionel42
Copy link
Owner

@AgainPsychoX Thanks that is quite a good example how the package could be improved.

Maybe if I find some time, I will try to make your example render "better" .

For the performance, I am not sure I would be able to do a lot, but it is also good to know how the package performs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants