First we import sys and add the path to pymunk. This is normally not required, but done here for convenience while developing Pymunk, as its not installed.


In [1]:
import sys
sys.path.insert(1,'c:/code/gh/pymunk')
sys.path.insert(1, 'c:/code/gh/pymunk/examples')

Next import matplotlib.pyplot. Nothing special here


In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
#from matplotlib import animation
#from IPython.display import HTML

Import the pymunk parts we need.


In [3]:
import pymunk
from pymunk.vec2d import Vec2d
from shapes_for_draw_demos import fill_space
import pymunk.matplotlib_util


Loading chipmunk for Windows (64bit) [c:\code\gh\pymunk\pymunk\chipmunk64.dll]

Create a pymunk space, and fill it with objects. For this example we use fill_space which is used by all the util drawing demos, so that its easy to compare the draw outputs between different bcakends (pygame, pyglet and matplotlib)


In [4]:
space = pymunk.Space()
captions = fill_space(space, (1,1,0,1))

Finally we draw the space on a axes. Note the use of set_aspect to ensure no scaling artifacts, and that we first create a DrawOptions instance with the ax and then call the space to draw itself using the newly created options.

First we make the drawing with white background, and then to show that the default colors works in black as well we invert the background.


In [7]:
fig = plt.figure(figsize=(14,10))
ax = plt.axes(xlim=(0, 1000), ylim=(0, 700))
ax.set_aspect("equal")
for caption in captions:
    x, y = caption[0]
    y = y - 15
    ax.text(x, y, caption[1], fontsize=12)
o = pymunk.matplotlib_util.DrawOptions(ax)
space.debug_draw(o)
fig.savefig("matplotlib_util_demo.png", bbox_inches="tight")


<cdata 'cpSpaceDebugColor &' 0x000001811A0511B4>

In [8]:
fig = plt.figure(figsize=(14,10))
ax = plt.axes(xlim=(0, 1000), ylim=(0, 700))
ax.set_aspect("equal")
ax.set_facecolor((0,0,0))
for caption in captions:
    x, y = caption[0]
    y = y - 15
    ax.text(x, y, caption[1], fontsize=12, color="white")

o = pymunk.matplotlib_util.DrawOptions(ax)
space.debug_draw(o)


<cdata 'cpSpaceDebugColor &' 0x000001811A051414>

In [ ]: