Skip to content

Commit

Permalink
documentated for uploading on pypi
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel42 committed Aug 1, 2021
1 parent 8b738c2 commit afc85b9
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
__pycache__

dist
*.egg-info
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,65 @@
This is an attempt to create a dedicated backend for matplotlib
in pygame.

The matplotlib ```Figure``` object is replaced by a ```FigureSurface``` object
which inherits from both ```matplotlib.figure.Figure``` and
```pygame.Surface```.

## Installation
```
pip install pygame-matplotlib
```

## Usage

First you will need to specify that you want to use pygame backend.
```
# Select pygame backend
import matplotlib
matplotlib.use('module://pygame_matplotlib.backend_pygame')
```

Then you can use matplotlib as you usually do.
```
# Standard matplotlib syntax
import matplotlib.pyplot as plt
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()
```

Or you can include the plot in your game using the fact that a ```Figure``` is
also a ```pygame.Surface``` with this backend.
```
import pygame
import pygame.display
fig, axes = plt.subplots(1, 1,)
axes.plot([1,2], [1,2], color='green', label='test')
fig.canvas.draw()
screen = pygame.display.set_mode((800, 600))
# Use the fig as a pygame.Surface
screen.blit(fig, (100, 100))
show = True
while show:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# Stop showing when quit
show = False
pygame.display.update()
```

Note that if you want to update the plot during the game, you might
need to call ```fig.canvas.draw()``` and ```screen.blit(fig)``` during
the game loop.

See examples in test.py or test_show.py

Still in progress ...
## Current implementation

Support mainly the basic plotting capabilities.

1 change: 1 addition & 0 deletions pygame_matplotlib/backend_pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):

previous_point = (0, 0)
poly_points = []
# print(path)
for point, code in path.iter_segments(transform):
# previous_point = point
# print(point, code)
Expand Down
11 changes: 6 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[metadata]
name = pygame-matplotlib
version = 0.0.1
version = 0.1
author = Lionel42
description = A matplotlib backend using pygame to show the plots.
description = A matplotlib backend using pygame.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/lionel42/pygame-matplotlib-backend
Expand All @@ -16,10 +16,11 @@ classifiers =
Operating System :: OS Independent

[options]
package_dir =
= pygame_matplotlib
packages = find:
install_requires =
matplotlib
pygame >= 2.0.0
python_requires = >=3.9

[options.packages.find]
where = pygame_matplotlib
include_package_data = False
26 changes: 26 additions & 0 deletions test_in_pygame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


import matplotlib
matplotlib.use('module://pygame_matplotlib.backend_pygame')
#matplotlib.use('Qt4Agg')

import matplotlib.pyplot as plt

import pygame
import pygame.display

fig, axes = plt.subplots(1, 1,)
axes.plot([1,2], [1,2], color='green', label='test')

fig.canvas.draw()

screen = pygame.display.set_mode((800, 600))
screen.blit(fig, (100, 100))

show = True
while show:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# Stop showing when quit
show = False
pygame.display.update()
4 changes: 2 additions & 2 deletions test_plt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pygame

import matplotlib
#matplotlib.use('module://pygame_matplotlib.backend_pygame')
matplotlib.use('Qt4Agg')
matplotlib.use('module://pygame_matplotlib.backend_pygame')
# matplotlib.use('Qt4Agg')

import matplotlib.pyplot as plt
import matplotlib.figure as fg
Expand Down
4 changes: 3 additions & 1 deletion test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
fig, axes = plt.subplots(3,2,figsize=(16, 12))
print(type(fig))

axes[0, 0].plot([1,2], [1,2], color='green')
axes[0, 0].plot([1,2], [1,2], color='green', label='test')
axes[0, 0].plot([1,2], [1,1], color='orange', label='test other')
# axes[0, 0].legend()
axes[0, 1].text(0.5, 0.5, '2', size=50)
axes[1, 0].set_xlabel('swag')
axes[1, 0].fill_between([0, 1, 2], [1, 2, 3], [3, 4, 5])
Expand Down

0 comments on commit afc85b9

Please sign in to comment.