prevent double call of _cleanup, which harms usefiles (and is a bad idea in general)
[PyX.git] / test / functional / test_graph.py
blob132bac4f890a417af718e971f8a9b54ddc566902
1 #!/usr/bin/env python
2 import sys; sys.path[:0] = ["../.."]
4 import math
5 from pyx import *
7 text.set(mode="latex")
9 def test_multiaxes_data(c, x, y):
10 g = c.insert(graph.graphxy(x, y, height=5, key=graph.key.key(pos="tl"),
11 x=graph.axis.log(title="$W$", manualticks=[graph.axis.tick.tick(math.sqrt(8)*100, label="?"), graph.axis.tick.tick(math.sqrt(8), label="$\sqrt{8}$")]),
12 y=graph.axis.log(title=r"$PPP_1$",
13 painter=graph.axis.painter.regular(titledirection=None)),
14 y2=graph.axis.log(title="$P_2$"),
15 y3=graph.axis.log(title="$PPP_3$",
16 painter=graph.axis.painter.regular(titledirection=graph.axis.painter.rotatetext(45), gridattrs=[color.gradient.RedGreen]),
17 texter=graph.axis.texter.decimal(equalprecision=1)),
18 y5=graph.axis.log(title="$P_5$")))
19 g.plot((graph.data.file("data/testdata", x=1, y="sqrt(sqrt($3))", title="mytitle"),
20 graph.data.file("data/testdata", x=1, y2=4),
21 graph.data.file("data/testdata", x=1, y3=5, title=None),
22 graph.data.file("data/testdata", x=1, y5=6)),
23 styles=[graph.style.symbol(symbolattrs=[deco.stroked.clear, color.gradient.RedGreen, graph.style.symbol.changestrokedfilled], symbol=graph.style.symbol.changesquaretwice)])
24 g.finish()
26 def test_piaxis_function(c, x, y):
27 xaxis=graph.axis.lin(min=0, max=2*math.pi, divisor=math.pi, texter=graph.axis.texter.rational(suffix=r"\pi"))
28 g = c.insert(graph.graphxy(x, y, height=5, x=xaxis))
29 #g = c.insert(graph.graphxy(x, y, height=5, x=xaxis, x2=xaxis))
30 g.plot([graph.data.function("y(x)=sin(x-i*pi/10)", context={"i": i}) for i in range(20)],
31 styles=[graph.style.line(lineattrs=[color.gradient.Hue])])
32 g.finish()
34 def test_textaxis_errorbars(c, x, y):
35 g = c.insert(graph.graphxy(x, y, height=5,
36 x=graph.axis.lin(min=0.5, max=12.5, parter=graph.axis.parter.lin("1", extendtick=None)),
37 y=graph.axis.lin(min=-10, max=30, title="Temperature [$^\circ$C]"),
38 x2=graph.axis.lin(painter=graph.axis.painter.regular(labelattrs=None)), y2=graph.axis.lin()))
39 g.plot(graph.data.file("data/testdata2", x=0, ymin="min", ymax="max"), [graph.style.errorbar()])
40 a = graph.style.symbol.triangle
41 g.plot(graph.data.paramfunction("k", 0, 2*math.pi, "x2, y2, dx2, dy2 = 0.8*sin(k), 0.8*cos(3*k), 0.05, 0.05"), [graph.style.symbol(symbol=a), graph.style.errorbar()])
42 g.finish()
44 def test_ownmark(c, x, y):
45 div = lambda x, y: int(x)/int(y)
46 mod = lambda x, y: int(x)%int(y)
47 g = c.insert(graph.graphxy(x, y, height=5, x=graph.axis.lin(min=0, max=10), y=graph.axis.lin(min=0, max=10)))
48 g.plot(graph.data.paramfunction("k", 0, 120, "x, y, size, angle = mod(k, 11), div(k, 11), (1+sin(k*pi/120))/2, 3*k", points=121, context=locals()), [graph.style.arrow()])
49 line1 = g.plot(graph.data.function("y(x)= 10/x"))
50 line2 = g.plot(graph.data.functionxy(lambda x: 12*x**-1.6))
51 line3 = g.plot(graph.data.function("y(x)=7/x"))
52 g.dolayout()
53 line4 = g.plot(graph.data.function("y(x)=25*x**-1.6"))
54 g.plot(graph.data.points([[-1, 1], [5, 2], [11, 5], [5, 11], [4, -1]], x=1, y=2), [graph.style.line(lineattrs=[color.rgb.red])])
55 g.finish()
57 p1=line1.path
58 p2=line2.path.reversed()
59 p3=line3.path.reversed()
60 p4=line4.path
61 (seg1a,), (seg2a,) = p1.intersect(p2)
62 (seg2b,), (seg3b,) = p2.intersect(p3)
63 (seg3c,), (seg4c,) = p3.intersect(p4)
64 (seg4d,), (seg1d,) = p4.intersect(p1)
65 area = p1.split([seg1a, seg1d])[1] << p4.split([seg4d, seg4c])[1] << p3.split([seg3c, seg3b])[1] << p2.split([seg2b, seg2a])[1]
66 area.normsubpaths[-1].close()
67 g.stroke(area, [style.linewidth.THick, deco.filled([color.gray(0.5)])])
69 def test_allerrorbars(c, x, y):
70 g = c.insert(graph.graphxy(x, y, height=5, width=5))
71 g.plot(graph.data.file("data/testdata3", x="x", y="y", xmin="xmin", xmax="xmax", ymin="ymin", ymax="ymax", text="text"), [graph.style.text(), graph.style.errorbar(), graph.style.symbol()])
72 g.finish()
74 def test_split(c, x, y):
75 g = c.insert(graph.graphxy(x, y, height=5, width=5,
76 x=graph.axis.log(),
77 y=graph.axis.split()))
78 g.plot(graph.data.file("data/testdata", x=1, y="splitatvalue($3, 0.005, 0.01, 0.015, 0.018)"))
79 g.finish()
81 def test_split2(c, x, y):
82 g = c.insert(graph.graphxy(x, y, height=5, width=5,
83 x=graph.axis.log(),
84 y=graph.axis.split(subaxes=[graph.axis.sizedlinear(size=0.005), graph.axis.sizedlinear(size=0.0025), graph.axis.autosizedlinear()], dist=0.0025)))
85 g.plot(graph.data.file("data/testdata", x=1, y="splitatvalue(0.0215-$3, 0.005, 0.01, 0.015, 0.015)"))
86 g.finish()
89 c = canvas.canvas()
90 test_multiaxes_data(c, 0, 21)
91 test_piaxis_function(c, 0, 14)
92 test_textaxis_errorbars(c, 0, 7)
93 test_ownmark(c, 0, 0)
94 test_allerrorbars(c, -7, 0)
95 test_split(c, -7, 7)
96 test_split2(c, -7, 14)
98 c.writeEPSfile("test_graph", page_paperformat=document.paperformat.A4)
99 c.writePDFfile("test_graph", page_paperformat=document.paperformat.A4)
100 c.writeSVGfile("test_graph", page_paperformat=document.paperformat.A4)