- adjustments to the new graph data+style handling
[PyX.git] / test / functional / test_graph.py
blobc048eb2b4fe5173883ad0a5a36cda43e55019491
1 #!/usr/bin/env python
2 import sys; sys.path[:0] = ["../.."]
4 import math
5 from pyx import *
6 from pyx import mathtree
8 text.set(mode="latex")
10 def test_multiaxes_data(c, x, y):
11 g = c.insert(graph.graphxy(x, y, height=5, key=graph.key.key(pos="tl"),
12 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}$")]),
13 y=graph.axis.log(title=r"$PPP_1$",
14 painter=graph.axis.painter.regular(titledirection=None)),
15 y2=graph.axis.log(title="$P_2$"),
16 y3=graph.axis.log(title="$PPP_3$",
17 painter=graph.axis.painter.regular(titledirection=graph.axis.painter.rotatetext(45), gridattrs=[color.palette.RedGreen]),
18 texter=graph.axis.texter.decimal(equalprecision=1)),
19 y5=graph.axis.log(title="$P_5$")))
20 g.plot((graph.data.file("data/testdata", x=1, y="sqrt(sqrt($3))", title="mytitle"),
21 graph.data.file("data/testdata", x=1, y2=4),
22 graph.data.file("data/testdata", x=1, y3=5, title=None),
23 graph.data.file("data/testdata", x=1, y5=6)),
24 styles=[graph.style.symbol(symbolattrs=[deco.stroked.clear, color.palette.RedGreen, graph.style.symbol.changestrokedfilled], symbol=graph.style.symbol.changesquaretwice)])
25 g.finish()
27 def test_piaxis_function(c, x, y):
28 xaxis=graph.axis.lin(min=0, max=2*math.pi, divisor=math.pi, texter=graph.axis.texter.rational(suffix=r"\pi"))
29 g = c.insert(graph.graphxy(x, y, height=5, x=xaxis))
30 #g = c.insert(graph.graphxy(x, y, height=5, x=xaxis, x2=xaxis)) # TODO
31 g.plot([graph.data.function("y=sin(x-i*pi/10)", context={"i": i}) for i in range(20)],
32 styles=[graph.style.line(lineattrs=[color.palette.Hue])])
33 g.finish()
35 def test_textaxis_errorbars(c, x, y):
36 g = c.insert(graph.graphxy(x, y, height=5,
37 x=graph.axis.lin(min=0.5, max=12.5, parter=graph.axis.parter.lin("1", extendtick=None)),
38 y=graph.axis.lin(min=-10, max=30, title="Temperature [$^\circ$C]"),
39 x2=graph.axis.lin(painter=graph.axis.painter.regular(labelattrs=None)), y2=graph.axis.lin()))
40 g.plot(graph.data.file("data/testdata2", x=0, ymin="min", ymax="max"), [graph.style.errorbar()])
41 a = graph.style.symbol.triangle
42 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()])
43 g.finish()
45 def test_ownmark(c, x, y):
46 div = lambda x, y: int(x)/int(y)
47 mod = lambda x, y: int(x)%int(y)
48 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)))
49 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()])
50 line1 = g.plot(graph.data.function("y=10/x"))
51 line2 = g.plot(graph.data.function("y=12*x**-1.6"))
52 line3 = g.plot(graph.data.function("y=7/x"))
53 line4 = g.plot(graph.data.function("y=25*x**-1.6"))
54 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])])
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((graph.axis.lin(min=0, max=0.005, painter=graph.axis.painter.regular()), graph.axis.lin(min=0.01, max=0.015), graph.axis.lin(min=0.02, max=0.025)), title="axis title", splitlist=(None, None), relsizesplitdist=0.005)))
78 g.plot(graph.data.file("data/testdata", x=1, y=3))
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((graph.axis.lin(max=0.002), graph.axis.lin(min=0.01, max=0.015), graph.axis.lin(min=0.017)), splitlist=(0.15, 0.75))))
85 g.plot(graph.data.file("data/testdata", x=1, y=3))
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", paperformat="a4")
99 c.writePDFfile("test_graph", paperformat="a4")