adds a separated module for plotting

This commit is contained in:
Johann Dreo 2014-03-20 23:29:41 +01:00
commit 3dadef14a4
2 changed files with 55 additions and 23 deletions

View file

@ -138,29 +138,6 @@ class DumpTurtleLSystem(TurtleLSystem):
return dump
def plot_segments( segments ):
import matplotlib.pyplot as plot
from matplotlib.path import Path
import matplotlib.patches as patches
fig = plot.figure()
ax = fig.add_subplot(111)
for segment in segments:
start,end = segment
verts = [start,end,(0,0)]
codes = [Path.MOVETO,Path.LINETO,Path.STOP]
path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='none', lw=1)
ax.add_patch(patch)
ax.set_xlim(-50,50)
ax.set_ylim(-50,50)
plot.show()
if __name__=="__main__":
import sys

55
uberplot.py Normal file
View file

@ -0,0 +1,55 @@
import sys
import argparse
import matplotlib.pyplot as plot
from matplotlib.path import Path
import matplotlib.patches as patches
def parse_segments( filename ):
segments = []
with open( filename ) as fd:
for line in fd:
edge = [ float(i) for i in line.split() ]
if len(edge) == 4:
start = (edge[0],edge[1])
end = (edge[2],edge[3])
segments.append( (start,end) )
return segments
def plot_segments( ax, segments, **kwargs ):
for start,end in segments:
verts = [start,end,(0,0)]
codes = [Path.MOVETO,Path.LINETO,Path.STOP]
path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='none', **kwargs )
ax.add_patch(patch)
def scatter_segments( ax, segments, color="black", alpha=1.0, linewidth=1.0 ):
xy = [ ((i[0],j[0]),(i[1],j[1])) for (i,j) in segments]
x = [i[0] for i in xy]
y = [i[1] for i in xy]
ax.scatter( x,y, s=20, marker='o', color=color, alpha=alpha, linewidth=linewidth)
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-s', "--segments", default=[None], action='store', type=str, nargs='*')
args = parser.parse_args()
fig = plot.figure()
ax = fig.add_subplot(111)
if args.segments != [None]:
for filename in args.segments:
seg = parse_segments(filename)
scatter_segments( ax, seg, edgecolor="red" )
plot_segments( ax, seg, edgecolor="blue" )
plot.show()