|
3 | 3 | This is an attempt to create a dedicated backend for matplotlib |
4 | 4 | in pygame. |
5 | 5 |
|
| 6 | +The matplotlib ```Figure``` object is replaced by a ```FigureSurface``` object |
| 7 | +which inherits from both ```matplotlib.figure.Figure``` and |
| 8 | +```pygame.Surface```. |
| 9 | + |
| 10 | +## Installation |
| 11 | +``` |
| 12 | +pip install pygame-matplotlib |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +First you will need to specify that you want to use pygame backend. |
| 18 | +``` |
| 19 | +# Select pygame backend |
| 20 | +import matplotlib |
| 21 | +matplotlib.use('module://pygame_matplotlib.backend_pygame') |
| 22 | +``` |
| 23 | + |
| 24 | +Then you can use matplotlib as you usually do. |
| 25 | +``` |
| 26 | +# Standard matplotlib syntax |
| 27 | +import matplotlib.pyplot as plt |
| 28 | +fig, ax = plt.subplots() # Create a figure containing a single axes. |
| 29 | +ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes. |
| 30 | +plt.show() |
| 31 | +``` |
| 32 | + |
| 33 | +Or you can include the plot in your game using the fact that a ```Figure``` is |
| 34 | +also a ```pygame.Surface``` with this backend. |
| 35 | +``` |
| 36 | +import pygame |
| 37 | +import pygame.display |
| 38 | +
|
| 39 | +fig, axes = plt.subplots(1, 1,) |
| 40 | +axes.plot([1,2], [1,2], color='green', label='test') |
| 41 | +
|
| 42 | +fig.canvas.draw() |
| 43 | +
|
| 44 | +screen = pygame.display.set_mode((800, 600)) |
| 45 | +
|
| 46 | +# Use the fig as a pygame.Surface |
| 47 | +screen.blit(fig, (100, 100)) |
| 48 | +
|
| 49 | +show = True |
| 50 | +while show: |
| 51 | + for event in pygame.event.get(): |
| 52 | + if event.type == pygame.QUIT: |
| 53 | + # Stop showing when quit |
| 54 | + show = False |
| 55 | + pygame.display.update() |
| 56 | +``` |
| 57 | + |
| 58 | +Note that if you want to update the plot during the game, you might |
| 59 | +need to call ```fig.canvas.draw()``` and ```screen.blit(fig)``` during |
| 60 | +the game loop. |
| 61 | + |
6 | 62 | See examples in test.py or test_show.py |
7 | 63 |
|
8 | | -Still in progress ... |
| 64 | +## Current implementation |
| 65 | + |
| 66 | +Support mainly the basic plotting capabilities. |
| 67 | + |
0 commit comments