Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using matplotlib in pygame

I have a big question here - one that I imagine the majority of people will not like and I will probably be flamed for being a noob but here goes anyway.

I am trying to use matplotlib to implement a barchart into my pygame program. I managed to do it pretty simply but not using pygame.

I have managed to make a bar chart but I am not able to put it into pygame:

names = ['a', 'b', 'c']
values = [1, 10, 100]
plt.figure(1, figsize=(9, 3))
plt.bar(names, values)
plt.show()

I want to implement this to my game in pygame but I honestly have no idea where to start. It is for a project due next week. Thanks.

like image 294
Deema Bowgen Avatar asked Jan 04 '18 10:01

Deema Bowgen


People also ask

Can you use matplotlib in Python?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.

Can you use matplotlib in terminal?

The best use of Matplotlib differs depending on how you are using it; roughly, the three applicable contexts are using Matplotlib in a script, in an IPython terminal, or in an IPython notebook.


2 Answers

matplotlib as default uses tkinter to display graph. It can also use PyQt or wxPython - they are called "backends" - but it doesn't have methods to use PyGame as backend.

The only what I found (using Google) is example in PyGame wiki

It renders matplotlib graph to bitmap and then converts this bitmap to string which can be used to create Surface with image.

import matplotlib
matplotlib.use("Agg")

import matplotlib.backends.backend_agg as agg


import pylab

fig = pylab.figure(figsize=[4, 4], # Inches
                   dpi=100,        # 100 dots per inch, so the resulting buffer is 400x400 pixels
                   )
ax = fig.gca()
ax.plot([1, 2, 4])

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

import pygame
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((600, 400), DOUBLEBUF)
screen = pygame.display.get_surface()

size = canvas.get_width_height()

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

crashed = False
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

enter image description here

Fankly I wouldn't use matplotlib to create graph - escpecially it is not interactive. It could be easy to create own graph in PyGame.

like image 86
furas Avatar answered Nov 15 '22 04:11

furas


You might want to take a look to this library that attempts to create a proper pygame backend for matplotlib. https://pypi.org/project/pygame-matplotlib/

It is under beta developpement for the moment, but looks promising.

like image 44
Oily Avatar answered Nov 15 '22 05:11

Oily