straight lines in bezier lentopar; some further small cleanups
[PyX/mjg.git] / test / functional / test_graph.py
blobbf326263d11861a916bed3a54d51e71cd30eff75
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(pos="tl"),
12 x=graph.logaxis(title="$W$", manualticks=[graph.tick(math.sqrt(8)*100, label="?"), graph.tick(math.sqrt(8), label="$\sqrt{8}$")]),
13 #x=graph.logaxis(title="$W$",
14 # texter=graph.decimaltexter()),
15 y=graph.logaxis(title=r"$PPP_1$",
16 painter=graph.axispainter(titledirection=None)),
17 y2=graph.logaxis(title="$P_2$"),
18 y3=graph.logaxis(title="$PPP_3$",
19 painter=graph.axispainter(titledirection=graph.rotatetext(45), gridattrs=([color.rgb.red], color.rgb.green)),
20 texter=graph.decimaltexter(equalprecision=1)),
21 y5=graph.logaxis(title="$P_5$")))
22 df = data.datafile("data/testdata")
23 g.plot((graph.data(df, x=1, y="sqrt(sqrt($3))"),
24 graph.data(df, x=1, y2=4),
25 graph.data(df, x=1, y3=5),
26 graph.data(df, x=1, y5=6)),
27 style=graph.symbol(symbolattrs=(graph.changecolor.RedGreen(), graph.changestrokedfilled()), symbol=graph.changesymbol.squaretwice()))
28 g.finish()
30 def test_piaxis_function(c, x, y):
31 xaxis=graph.linaxis(min=0, max=2*math.pi, divisor=math.pi, texter=graph.rationaltexter(suffix=r"\pi"))
32 g = c.insert(graph.graphxy(x, y, height=5, x=xaxis))
33 # g = c.insert(graph.graphxy(x, y, height=5, x=xaxis, x2=xaxis)) # TODO
34 g.plot([graph.function("y=sin(x-i*pi/10)", context={"i": i}) for i in range(20)],
35 style=graph.line(lineattrs=(graph.changecolor.Hue(), graph.changelinestyle())))
36 g.finish()
38 def test_textaxis_errorbars(c, x, y):
39 df = data.datafile("data/testdata2")
40 g = c.insert(graph.graphxy(x, y, height=5,
41 x=graph.linaxis(min=0.5, max=12.5, title="Month",
42 parter=graph.linparter("1", labels=df.getcolumn("month"), extendtick=None),
43 painter=graph.axispainter(labeldist=0.1, titledist=0, labelattrs=(trafo.rotate(45),text.halign.right, text.size.scriptsize))),
44 y=graph.linaxis(min=-10, max=30, title="Temperature [$^\circ$C]"),
45 x2=graph.linaxis(), y2=graph.linaxis()))
46 g.plot(graph.data(df, x=0, ymin="min", ymax="max"))
47 g.plot(graph.paramfunction("k", 0, 2*math.pi, "x2, y2, dx2, dy2 = 0.8*sin(k), 0.8*cos(3*k), 0.05, 0.05"), style = graph.symbol(symbol=graph.symbol.triangle))
48 g.finish()
50 def test_ownmark(c, x, y):
51 div = lambda x, y: int(x)/int(y)
52 mod = lambda x, y: int(x)%int(y)
53 g = c.insert(graph.graphxy(x, y, height=5, x=graph.linaxis(min=0, max=10), y=graph.linaxis(min=0, max=10)))
54 g.plot(graph.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()), style = graph.arrow())
55 line1 = g.plot(graph.function("y=10/x")).style
56 line2 = g.plot(graph.function("y=12*x^-1.6")).style
57 line3 = g.plot(graph.function("y=7/x")).style
58 line4 = g.plot(graph.function("y=25*x^-1.6")).style
59 g.finish()
61 p1=line1.path
62 p2=line2.path.reversed()
63 p3=line3.path.reversed()
64 p4=line4.path
65 (seg1a,), (seg2a,) = p1.intersect(p2)
66 (seg2b,), (seg3b,) = p2.intersect(p3)
67 (seg3c,), (seg4c,) = p3.intersect(p4)
68 (seg4d,), (seg1d,) = p4.intersect(p1)
69 area = p1.split([seg1a, seg1d])[1] << p4.split([seg4d, seg4c])[1] << p3.split([seg3c, seg3b])[1] << p2.split([seg2b, seg2a])[1]
70 area.append(path.closepath())
71 g.stroke(area, [style.linewidth.THick, deco.filled([color.gray(0.5)])])
73 def test_allerrorbars(c, x, y):
74 df = data.datafile("data/testdata3")
75 g = c.insert(graph.graphxy(x, y, height=5, width=5))
76 g.plot(graph.data(df, x="x", y="y", xmin="xmin", xmax="xmax", ymin="ymin", ymax="ymax", text="text"), graph.text())
77 g.finish()
79 #def test_3d(c, x, y):
81 # class Div(mathtree.MathTreeFunc2):
82 # def __init__(self, *args):
83 # mathtree.MathTreeFunc2.__init__(self, "div", *args)
84 # def Calc(self, VarDict):
85 # return divmod(self.ArgV[0].Calc(VarDict), self.ArgV[1].Calc(VarDict))[0]
87 # class Mod(mathtree.MathTreeFunc2):
88 # def __init__(self, *args):
89 # mathtree.MathTreeFunc2.__init__(self, "mod", *args)
90 # def Calc(self, VarDict):
91 # return divmod(self.ArgV[0].Calc(VarDict), self.ArgV[1].Calc(VarDict))[1]
93 # MyFuncs = mathtree.DefaultMathTreeFuncs + (Div, Mod)
95 # g = c.insert(graph.graphxyz(x, y, height=5, width=5, depth=5,
96 # x=graph.linaxis(min=0, max=10, painter=graph.axispainter(baselineattrs=color.rgb.red)),
97 # y=graph.linaxis(min=0, max=10, painter=graph.axispainter(baselineattrs=color.rgb.green)),
98 # z=graph.linaxis(min=0, max=10, painter=graph.axispainter(baselineattrs=color.rgb.blue))))
99 # g.plot(graph.paramfunction("k", 0, 120, "x, y, z = mod(k, 11), div(k, 11), exp(-0.1*(mod(k, 11)-5)*(mod(k, 11)-5)-0.1*(div(k, 11)-5)*(div(k, 11)-5))", points=121, parser=mathtree.parser(MathTreeFuncs=MyFuncs)), style = graph.surface())
100 # g.finish()
102 def test_split(c, x, y):
103 g = c.insert(graph.graphxy(x, y, height=5, width=5,
104 x=graph.logaxis(),
105 #y=graph.splitaxis((graph.linaxis(max=0.002), graph.splitaxis((graph.linaxis(min=0.01, max=0.015), graph.linaxis(min=0.017)))))))
106 #y=graph.splitaxis((graph.linaxis(max=0.002), graph.linaxis(min=0.01, max=0.015), graph.linaxis(min=0.017)), splitlist=(0.15, 0.75))))
107 y=graph.splitaxis((graph.linaxis(min=0, max=0.005, painter=graph.axispainter()), graph.linaxis(min=0.01, max=0.015), graph.linaxis(min=0.02, max=0.025)), title="axis title", splitlist=(None, None), relsizesplitdist=0.005)))
108 df = data.datafile("data/testdata")
109 g.plot(graph.data(df, x=1, y=3))
110 g.finish()
112 def test_bar(c, x, y):
113 df = data.datafile("data/testdata2")
114 g = c.insert(graph.graphxy(x, y, height=5, width=5, x=graph.baraxis(title="Month", painter=graph.baraxispainter(nameattrs=(text.halign.right, text.vshift.mathaxis, trafo.rotate(90))))))
115 g.plot(graph.data(df, x=1, y=2), graph.bar(fromzero=0))
116 #g = c.insert(graph.graphxy(x, y, height=5, width=5, y=graph.baraxis(title="Month")))
117 #g.plot(graph.data(df, x=2, y=1), graph.bar(xbar=1, fromzero=0))
118 #g = c.insert(graph.graphxy(x, y, height=5, width=20, x=graph.baraxis(multisubaxis=graph.baraxis(dist=0), painter=graph.baraxispainter(innerticklength=0.3))))
119 #g.plot([graph.data(df, x=1, y=2), graph.data(df, x=1, y=3), graph.data(df, x=1, y=3)], graph.bar())
120 #g = c.insert(graph.graphxy(x, y, height=5, width=20, x=graph.baraxis(graph.baraxis(dist=0))))
121 #g.plot([graph.data(df, x=0, y=2), graph.data(df, x=0, y=3), graph.data(df, x=0, y=2), None, graph.data(df, x=0, y=3)], graph.bar(stacked=2))
122 g.finish()
124 c = canvas.canvas()
125 test_multiaxes_data(c, 0, 21)
126 test_piaxis_function(c, 0, 14)
127 test_textaxis_errorbars(c, 0, 7)
128 test_ownmark(c, 0, 0)
129 test_allerrorbars(c, -7, 0)
130 ##test_3d(c, -7, 7)
131 test_split(c, -7, 7)
132 test_bar(c, -7, 14)
134 c.writetofile("test_graph", paperformat="a4")