formats->format
[PyX/mjg.git] / gallery / graphs / partialfill.py
blob48aceb314344ba530563ad063975642802640a98
1 # contributed by Michael Schindler
3 from pyx import *
5 # get the lines from the graph
6 xax = graph.axis.linear(min=-1, max=1.0, painter=None)
7 yax = graph.axis.linear(min=-1.3, max=1.3, painter=None)
8 g = graph.graphxy(width=10, ratio=2, x=xax, y=yax)
9 pifline = g.plot(graph.data.function("y(x)=sin(1.0/(x**2+0.02122))", points=1000))
10 pihoriz = g.plot(graph.data.function("y(x)=0.5*x", points=2))
11 g.finish()
13 # fetch path from info returned by plot method of graph
14 fline = pifline.path
15 horiz = pihoriz.path
17 # intersect the lines
18 splith, splitf = horiz.intersect(fline)
20 # create gray area (we do not use simple clipping)
21 area = horiz.split([splith[0]])[0]
22 for i in range(0, len(splith)-2, 2):
23 area = area.joined(fline.split([splitf[i], splitf[i+1]])[1])
24 area = area.joined(horiz.split([splith[i+1], splith[i+2]])[1])
25 area = area.joined(fline.split([splitf[-2], splitf[-1]])[1])
26 area = area.joined(horiz.split([splith[-1]])[1])
27 area.append(path.lineto(*g.vpos(1, 0)))
28 area.append(path.lineto(*g.vpos(0, 0)))
29 area.append(path.closepath()) # not really needed (filling closes automatically)
31 c = canvas.canvas()
33 # draw first the area, then the function
34 c.fill(area, [color.gray(0.6)])
35 c.stroke(fline, [style.linewidth.Thick, style.linejoin.round])
37 c.writeEPSfile("partialfill")
38 c.writePDFfile("partialfill")