Dear Matplotlib folks,

I want to plot all paths of a simple random walk and wrote the following
recursive program based on the Path tutorial [1].

        import matplotlib.pyplot as plt
        from matplotlib.path import Path
        import matplotlib.patches as patches
        
        def draw(a, b, c, d):
                        verts = [
                        (a, b),
                        (c, d),
                        (0, 0),
                        ]
        
                        codes = [
                        Path.MOVETO,
                        Path.LINETO,
                        Path.CLOSEPOLY
                        ]
        
                        path = Path(verts, codes)
                        patch = patches.PathPatch(path)
                        ax.add_patch(patch)
        
        def irrpfad(a, b):
                if a < length:
                        draw(a, b, a + 1., b + 1.)
                        draw(a, b, a + 1., b - 1.)
                        irrpfad(a + 1, b + 1)
                        irrpfad(a + 1, b - 1)
        
        
        length = 5 # 20 not possible to run
        
        fig = plt.figure()
        ax = fig.add_subplot(111)
        irrpfad(0, 0)
        ax.set_xlim(0,length)
        ax.set_ylim(-length,length)
        plt.show()

Using 20 for `length` stalls my system and the memory used seems to be
over 1 GB. I guess this is what you guess using something recursive.
What optimizations are there. I am drawing each line after another so
probably too many separate paths instead of one. Being a Python noob I
do not know if I can append something to a path. Looking at the API
documentation [2] I did not see such a method.

Being also new to Matplotlib I may have also overlooked more appropriate
methods/classes.

So to summarize my message,

1. How can I add lines to a path?
2. Are recursive functions bad in Python/Matplotlib?
3. Are there better approaches?

Please find the source also attached. I am using python-matplotlib
1.0.1-2 from Debian Sid/unstable.


Thanks,

Paul


[1] http://matplotlib.sourceforge.net/users/path_tutorial.html
[2] http://matplotlib.sourceforge.net/api/path_api.html#matplotlib.path.Path
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

def draw(a, b, c, d):
                verts = [
                (a, b),
                (c, d),
                (0, 0),
                ]

                codes = [
                Path.MOVETO,
                Path.LINETO,
                Path.CLOSEPOLY
                ]

                path = Path(verts, codes)
                patch = patches.PathPatch(path)
                ax.add_patch(patch)

def irrpfad(a, b):
        if a < length:
                draw(a, b, a + 1., b + 1.)
                draw(a, b, a + 1., b - 1.)
                irrpfad(a + 1, b + 1)
                irrpfad(a + 1, b - 1)


length = 5 # 20 not possible to run

fig = plt.figure()
ax = fig.add_subplot(111)
irrpfad(0, 0)
ax.set_xlim(0,length)
ax.set_ylim(-length,length)
plt.show()

Attachment: signature.asc
Description: This is a digitally signed message part

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to