import PyArt,whrandom, math w = 356 h = w* (2/3.0) # width and height of canvas canvas = PyArt.Canvas(w,h, bg=0x7f7f7f) # set up coordinate system so it start in lower left hand corner and y increases # upwards canvas.translate(0,h) canvas.scale(1,-1) canvas.gsave() #x0, y0 = 85.3333, 85.3333 # let this be the origin of the graph x0, y0 = .20*w, .25*h # let this be the origin of the graph canvas.translate(x0,y0) # move to origin of graph # define paths for x and y axes yaxis = PyArt.Path() y_range = h-2*y0 # for now yaxis.moveto_open(0,0) yaxis.lineto(0, y_range) yaxis.end() x_range = w - 2*x0 xaxis = PyArt.Path() xaxis.moveto_open(0,0) xaxis.lineto(x_range,0) xaxis.end() canvas.gstate.stroke= 0x000000 canvas.stroke(yaxis) canvas.stroke(xaxis) # draw x-axis label canvas.gstate.font_size = 16 # ~16 pixels high canvas.gsave() canvas.translate(x_range/2.0-15, -20) # need to subtract off 1/2 width of string canvas.gstate.fill = 0x000000 canvas.drawString(0,0, "time", flipY=0) canvas.grestore() # y-axis label canvas.gsave() canvas.translate(-20, y_range/2.0 - 25) canvas.rotate(-90) canvas.gstate.fill = 0x000000 canvas.drawString(0,0,"response", flipY=0) canvas.grestore() canvas.translate(5,5) # plot a random line with a small quadratic dependence print "Generating data" # cover most of x range x_end = x_range*.95 y_end = y_range*.87 totpt = 50 xs = x_end/totpt ys = y_end/x_end data = PyArt.Path(totpt+2) data.moveto_open(0.0,0.0) for ii in range(totpt): x = ii * xs delta = whrandom.random() * y_range y = (0.5)*ys * x + 0.5*delta + 15* math.sin(.04*x) # y = linear + sin + random #print x,y data.lineto(x,y) data.end() canvas.gstate.stroke_width = 1.2 canvas.gstate.stroke = 0x00FF7f canvas.gstate.stroke_opacity = 1.0 canvas.stroke(data) canvas.save("demo2.png")