Generate a sin wave svg in Python

To set this up, you need do install pysvg:

pip install pysvg

The following sets up a sin wave svg with Python:

import pysvg.structure
import pysvg.builders
import pysvg.text
import math

svg_document = pysvg.structure.Svg()

shape_builder = pysvg.builders.ShapeBuilder()

style = pysvg.builders.StyleBuilder()
style.setStrokeWidth(2)
style.setStroke('black')

def line(a, b):
    if (a == None):
        return
    if (b == None):
        return
    (x1, y1) = a
    (x2, y2) = b
    print x1
    line = shape_builder.createLine(x1, y1, x2, y2, style)
    svg_document.addElement(line)

lines = [(0, 100), (20, 300), (0, 10), (100, 0)]

pre = None
for id in xrange(1000):
    point = (id, 100.0 + 100.0 * math.sin(id/20.0))
    line(point, pre)
    pre = point
    
print(svg_document.getXML())

svg_document.save("Documents/test-pysvg.svg")

Leave a Reply

Your email address will not be published.