Skip to content

Commit afc85b9

Browse files
committed
documentated for uploading on pypi
1 parent 8b738c2 commit afc85b9

File tree

7 files changed

+101
-9
lines changed

7 files changed

+101
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
__pycache__
2+
3+
dist
4+
*.egg-info

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,65 @@
33
This is an attempt to create a dedicated backend for matplotlib
44
in pygame.
55

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+
662
See examples in test.py or test_show.py
763

8-
Still in progress ...
64+
## Current implementation
65+
66+
Support mainly the basic plotting capabilities.
67+

pygame_matplotlib/backend_pygame.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
5757

5858
previous_point = (0, 0)
5959
poly_points = []
60+
# print(path)
6061
for point, code in path.iter_segments(transform):
6162
# previous_point = point
6263
# print(point, code)

setup.cfg

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[metadata]
22
name = pygame-matplotlib
3-
version = 0.0.1
3+
version = 0.1
44
author = Lionel42
5-
description = A matplotlib backend using pygame to show the plots.
5+
description = A matplotlib backend using pygame.
66
long_description = file: README.md
77
long_description_content_type = text/markdown
88
url = https://github.com/lionel42/pygame-matplotlib-backend
@@ -16,10 +16,11 @@ classifiers =
1616
Operating System :: OS Independent
1717

1818
[options]
19-
package_dir =
20-
= pygame_matplotlib
2119
packages = find:
20+
install_requires =
21+
matplotlib
22+
pygame >= 2.0.0
2223
python_requires = >=3.9
2324

2425
[options.packages.find]
25-
where = pygame_matplotlib
26+
include_package_data = False

test_in_pygame.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
import matplotlib
4+
matplotlib.use('module://pygame_matplotlib.backend_pygame')
5+
#matplotlib.use('Qt4Agg')
6+
7+
import matplotlib.pyplot as plt
8+
9+
import pygame
10+
import pygame.display
11+
12+
fig, axes = plt.subplots(1, 1,)
13+
axes.plot([1,2], [1,2], color='green', label='test')
14+
15+
fig.canvas.draw()
16+
17+
screen = pygame.display.set_mode((800, 600))
18+
screen.blit(fig, (100, 100))
19+
20+
show = True
21+
while show:
22+
for event in pygame.event.get():
23+
if event.type == pygame.QUIT:
24+
# Stop showing when quit
25+
show = False
26+
pygame.display.update()

test_plt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import pygame
55

66
import matplotlib
7-
#matplotlib.use('module://pygame_matplotlib.backend_pygame')
8-
matplotlib.use('Qt4Agg')
7+
matplotlib.use('module://pygame_matplotlib.backend_pygame')
8+
# matplotlib.use('Qt4Agg')
99

1010
import matplotlib.pyplot as plt
1111
import matplotlib.figure as fg

test_show.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
fig, axes = plt.subplots(3,2,figsize=(16, 12))
1818
print(type(fig))
1919

20-
axes[0, 0].plot([1,2], [1,2], color='green')
20+
axes[0, 0].plot([1,2], [1,2], color='green', label='test')
21+
axes[0, 0].plot([1,2], [1,1], color='orange', label='test other')
22+
# axes[0, 0].legend()
2123
axes[0, 1].text(0.5, 0.5, '2', size=50)
2224
axes[1, 0].set_xlabel('swag')
2325
axes[1, 0].fill_between([0, 1, 2], [1, 2, 3], [3, 4, 5])

0 commit comments

Comments
 (0)