Sunday, September 19, 2010
No recent posts
I have not posted a thing in a long time. Did not mean for that to happen. I've been getting a project off my back that I hope will pay off a little bit. It is certain now that I will have to get into web dev stuff.
Sunday, June 13, 2010
Rose curves in matplotlib
Specifically because this image
Rose curves are just the polar equations of the sort
r = a * sin((n / d) * theta)
Where a (normally 1.0) determines length of petals, and n/d (normally d = 1) determine shape of petals as the equation is plotted across the domain of the sin call.
I thought it would be cool to write a python script that lets the user play with the numerator / denominator of the coefficient, and also the colors as hex triplets preceded by a hash mark, like #addfad or #00bd39 .
Here's all the code, blogger eats my indents so watch out folks
#!/usr/bin/python
import os, sys, getopt
from matplotlib.pyplot import show, plot, savefig, box, xticks, yticks
from numpy import arange, cos, sin, pi
import time
import optparse
def main():
parser = optparse.OptionParser("usage: %prog [options]")
parser.add_option("--color", dest = "color",
default = "#aaaaaa", type = "string",
help = "line color")
parser.add_option("--bgcolor", dest = "bgcolor",
default = "#444444", type = "string",
help = "background color, void if transparent")
parser.add_option("-t", dest = "trans",
action = "store_true",
help = "set transparent bg")
parser.add_option("-n", dest = "n",
default = 1, type = int,
help = "numerator of coefficient")
parser.add_option("-d", dest = "d",
default = 1, type = int,
help = "denominator of coefficient")
(options, args) = parser.parse_args()
color = options.color
bgcolor = options.bgcolor
trans = options.trans
n = options.n
d = options.d
mktime = time.time()
if not os.path.exists('roseout'):
os.mkdir('roseout')
outfile = 'roseout/' + str(n) + "_" + str(d) + "_" + str(mktime)
if n < 1:
n = 1
if d < 1:
d = 1
n = int(n)
d = int(d)
n = float(n)
d = float(d)
k = n / d
theta = arange(0.0, d * 2.0 * pi, 0.01)
r = sin(k * theta)
x = r * cos (theta)
y = r * sin (theta)
xticks([])
yticks([])
box(on=None)
plot(x, y, color = color)
savefig(outfile + ".png", format="png", transparent = trans, facecolor=bgcolor)
if __name__ == "__main__":
main()
You use it by saving it as 'rose' or whatever (chmod'ing it to 744 or anything that lets you execute it), changing to the directory and typing
./rose -n 9 -d 2 --color "#FFFFFF" --bgcolor "#000000"
changing the integers and hex triplets to your own preferences.
I had to manually convert polar coordinates to x,y using the plot() call, rather than pyplot's polar() call, because somehow polar() was only plotting part of my shape. I had some help from redditor 'desrosiers' with minor math stuff and my arange() calls.
Subscribe to:
Posts (Atom)