make columnnames static; provide texrunner to anchoraxis
[PyX.git] / test / functional / test_graph.py
blob6d8ee8e8965c5cab688a1f3279b01050593e0842
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.palette.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.palette.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.palette.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.function("y(x)=12*x**-1.6"))
51 line3 = g.plot(graph.data.function("y(x)=7/x"))
52 line4 = g.plot(graph.data.function("y(x)=25*x**-1.6"))
53 g.plot(graph.data.list([[-1, 1], [5, 2], [11, 5], [5, 11], [4, -1]], x=1, y=2), [graph.style.line(lineattrs=[color.rgb.red])])
54 g.finish()
56 p1=line1.path
57 p2=line2.path.reversed()
58 p3=line3.path.reversed()
59 p4=line4.path
60 (seg1a,), (seg2a,) = p1.intersect(p2)
61 (seg2b,), (seg3b,) = p2.intersect(p3)
62 (seg3c,), (seg4c,) = p3.intersect(p4)
63 (seg4d,), (seg1d,) = p4.intersect(p1)
64 area = p1.split([seg1a, seg1d])[1] << p4.split([seg4d, seg4c])[1] << p3.split([seg3c, seg3b])[1] << p2.split([seg2b, seg2a])[1]
65 area.normsubpaths[-1].close()
66 g.stroke(area, [style.linewidth.THick, deco.filled([color.gray(0.5)])])
68 def test_allerrorbars(c, x, y):
69 g = c.insert(graph.graphxy(x, y, height=5, width=5))
70 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()])
71 g.finish()
73 def test_split(c, x, y):
74 g = c.insert(graph.graphxy(x, y, height=5, width=5,
75 x=graph.axis.log(),
76 y=graph.axis.split()))
77 g.plot(graph.data.file("data/testdata", x=1, y="splitatvalue($3, 0.005, 0.01, 0.015, 0.018)"))
78 g.finish()
80 def test_split2(c, x, y):
81 g = c.insert(graph.graphxy(x, y, height=5, width=5,
82 x=graph.axis.log(),
83 y=graph.axis.split(subaxes=[graph.axis.sizedlinear(size=0.005), graph.axis.sizedlinear(size=0.0025), graph.axis.autosizedlinear()], dist=0.0025)))
84 g.plot(graph.data.file("data/testdata", x=1, y="splitatvalue(0.0215-$3, 0.005, 0.01, 0.015, 0.015)"))
85 g.finish()
88 c = canvas.canvas()
89 test_multiaxes_data(c, 0, 21)
90 test_piaxis_function(c, 0, 14)
91 test_textaxis_errorbars(c, 0, 7)
92 test_ownmark(c, 0, 0)
93 test_allerrorbars(c, -7, 0)
94 test_split(c, -7, 7)
95 test_split2(c, -7, 14)
97 c.writeEPSfile("test_graph", paperformat=document.paperformat.A4)
98 c.writePDFfile("test_graph", paperformat=document.paperformat.A4)