File: partialfill.py

package info (click to toggle)
pyx 0.10-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,316 kB
  • ctags: 2,779
  • sloc: python: 16,243; ansic: 131; makefile: 87; sh: 9
file content (38 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# contributed by Michael Schindler

from pyx import *

# get the lines from the graph
xax = graph.axis.linear(min=-1, max=1.0, painter=None)
yax = graph.axis.linear(min=-1.3, max=1.3, painter=None)
g = graph.graphxy(width=10, ratio=2, x=xax, y=yax)
pifline = g.plot(graph.data.function("y(x)=sin(1.0/(x**2+0.02122))", points=1000))
pihoriz = g.plot(graph.data.function("y(x)=0.5*x", points=2))
g.finish()

# fetch path from info returned by plot method of graph
fline = pifline.path
horiz = pihoriz.path

# intersect the lines
splith, splitf = horiz.intersect(fline)

# create gray area (we do not use simple clipping)
area = horiz.split([splith[0]])[0]
for i in range(0, len(splith)-2, 2):
    area = area.joined(fline.split([splitf[i], splitf[i+1]])[1])
    area = area.joined(horiz.split([splith[i+1], splith[i+2]])[1])
area = area.joined(fline.split([splitf[-2], splitf[-1]])[1])
area = area.joined(horiz.split([splith[-1]])[1])
area.append(path.lineto(*g.vpos(1, 0)))
area.append(path.lineto(*g.vpos(0, 0)))
area.append(path.closepath()) # not really needed (filling closes automatically)

c = canvas.canvas()

# draw first the area, then the function
c.fill(area, [color.gray(0.6)])
c.stroke(fline, [style.linewidth.Thick, style.linejoin.round])

c.writeEPSfile("partialfill")
c.writePDFfile("partialfill")