From 19fbef74064bd6b52f93ca4ff07ea98ff626eee3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Wobst?= Date: Fri, 20 Feb 2004 09:31:21 +0000 Subject: [PATCH] new graph styles: cleanups of examples, manual etc. git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@1347 069f4177-920e-0410-937b-c2a4a81bcd90 --- examples/graphs/bar.py | 8 +++----- examples/graphs/change.py | 2 +- examples/graphs/integral.py | 9 +++++---- examples/graphs/partialfill.py | 6 +++--- manual/palettename.py | 2 +- pyx/__init__.py | 2 +- pyx/color.py | 11 ++++++----- pyx/graph.py | 44 ++++++++++++++++++++++-------------------- test/functional/test_graph.py | 8 ++++---- 9 files changed, 47 insertions(+), 45 deletions(-) diff --git a/examples/graphs/bar.py b/examples/graphs/bar.py index 65ac3234..b8af7600 100644 --- a/examples/graphs/bar.py +++ b/examples/graphs/bar.py @@ -11,13 +11,11 @@ a2 = graph.baraxis(painter=bap(nameattrs=[trafo.rotate(45), subaxis=graph.baraxis(dist=0)) # for several bars df = data.datafile("bar.dat") # read the datafile just once d = [graph.data(df, x="month", y="min"), graph.data(df, x=1, y=3)] -nofirst = (graph.changesequence(None, deco.stroked([color.gray.black])), - graph.changesequence(None, color.rgb.green)) # special draw attrs +nofirst = [attr.changelist([None, color.rgb.green])] # special draw attrs c = canvas.canvas() # we draw several plots, thus we create a main canvas g = c.insert(graph.graphxy(ypos=4.5, width=8, height=4, x=a1)) -g.plot(d, graph.bar(stacked=1, barattrs=nofirst)) -g.finish() # we explicitly finish the plot allow for a reuse of "d" +g.plot(graph.data(df, xname=1, y=2, ystack1=3), graph.bar(barattrs=nofirst)) g2 = c.insert(graph.graphxy(width=8, x=a2, height=4)) -g2.plot(d, graph.bar()) +g2.plot([graph.data(df, xname=1, y=2), graph.data(df, xname=1, y=3)], graph.bar()) c.writetofile("bar") diff --git a/examples/graphs/change.py b/examples/graphs/change.py index 6bb0484b..fbe929f3 100644 --- a/examples/graphs/change.py +++ b/examples/graphs/change.py @@ -5,6 +5,6 @@ g = graph.graphxy(width=10, y=graph.linaxis(min=0, max=2)) g.plot([graph.function("x=y**(2**(3-%i))" % i) for i in range(3)] + [graph.function("y=x**(2**%i)" % i) for i in range(4)], - graph.line([graph.changecolor.Rainbow()])) + graph.line([color.palette.Rainbow])) g.writetofile("change") diff --git a/examples/graphs/integral.py b/examples/graphs/integral.py index 07a58dd7..4ecdb57f 100644 --- a/examples/graphs/integral.py +++ b/examples/graphs/integral.py @@ -11,15 +11,16 @@ g = graph.graphxy(width=8, x2=None, y2=None, parter=None, painter=p), y=graph.linaxis(title="$y$", parter=None, painter=p)) -style = g.plot(graph.function("y=(x-3)*(x-5)*(x-7)")).style +d = g.plot(graph.function("y=(x-3)*(x-5)*(x-7)")) g.finish() +p = d.path # the path is available after the graph is finished pa = g.xgridpath(a) pb = g.xgridpath(b) -(splita,), (splitpa,) = style.path.intersect(pa) -(splitb,), (splitpb,) = style.path.intersect(pb) +(splita,), (splitpa,) = p.intersect(pa) +(splitb,), (splitpb,) = p.intersect(pb) area = (pa.split([splitpa])[0] << - style.path.split([splita, splitb])[1] << + p.split([splita, splitb])[1] << pb.split([splitpb])[0].reversed()) area.append(path.closepath()) g.stroke(area, [deco.filled([color.gray(0.8)])]) diff --git a/examples/graphs/partialfill.py b/examples/graphs/partialfill.py index 2f89256d..42bb2647 100644 --- a/examples/graphs/partialfill.py +++ b/examples/graphs/partialfill.py @@ -8,11 +8,11 @@ yax = graph.linaxis(min=-1.3, max=1.3, painter=None) g = graph.graphxy(width=10, ratio=2, x=xax, y=yax) fline = g.plot(graph.function("y=sin(1.0/(x**2+0.02122))", points=1000)) horiz = g.plot(graph.function("y=0.5*x", points=2)) -g.dodata() +g.finish() # convert paths to normpaths (for efficiency reasons only) -fline = path.normpath(fline.style.path) -horiz = path.normpath(horiz.style.path) +fline = path.normpath(fline.path) +horiz = path.normpath(horiz.path) # intersect the lines splith, splitf = horiz.intersect(fline) diff --git a/manual/palettename.py b/manual/palettename.py index 2c45f788..b0660271 100755 --- a/manual/palettename.py +++ b/manual/palettename.py @@ -34,7 +34,7 @@ for line in lines: # we yet don't use a file iterator else: x2axis=graph.linkaxis(xaxis, painter=graph.linkaxispainter(innerticklength=None)) g = c.insert(graph.graphxy(ypos=y, width=10, height=0.5, x=xaxis, x2=x2axis, y=graph.linaxis(parter=None))) - g.plot(pf, graph.rect(pyx.color.palette.__dict__[m.group("name")])) + g.plot(pf, graph.rect(getattr(pyx.color.palette, m.group("name")))) g.dodata() g.finish() c.text(10.2, y + 0.15, m.group("id"), [text.size.footnotesize]) diff --git a/pyx/__init__.py b/pyx/__init__.py index 30907ab5..3b1f6dbb 100644 --- a/pyx/__init__.py +++ b/pyx/__init__.py @@ -34,7 +34,7 @@ import version __version__ = version.version -__all__ = ["box", "canvas", "color", "connector", "data", "deco", "epsfile", "graph", "path", +__all__ = ["attr", "box", "canvas", "color", "connector", "data", "deco", "epsfile", "graph", "path", "style", "trafo", "tex", "text", "unit"] # automatically import main modules into pyx namespace diff --git a/pyx/color.py b/pyx/color.py index 1578d1be..000ad82d 100644 --- a/pyx/color.py +++ b/pyx/color.py @@ -170,11 +170,12 @@ cmyk.white = cmyk.White cmyk.black = cmyk.Black -class palette(attr.changeattr): +class palette(color, attr.changeattr): """palette is a collection of two colors for calculating transitions between them""" def __init__(self, mincolor, maxcolor, min=0, max=1): + color.__init__(self) if mincolor.__class__ != maxcolor.__class__: raise ValueError self.colorclass = mincolor.__class__ @@ -191,10 +192,10 @@ class palette(attr.changeattr): return self.colorclass(**color) def select(self, index, total): - if total > 1: - return self.getcolor(index/(total-1.0)) - else: - return self.getcolor(0) + return self.getcolor(index/(total-1.0)) + + def write(self, file): + self.getcolor(0).write(file) palette.Gray = palette(gray.white, gray.black) diff --git a/pyx/graph.py b/pyx/graph.py index 559c1e3c..a867514e 100644 --- a/pyx/graph.py +++ b/pyx/graph.py @@ -1537,7 +1537,8 @@ class pathaxispos(_axispos): def __init__(self, p, convert, direction=1): self.path = p self.normpath = path.normpath(p) - self.arclength = self.normpath.arclength() + self.arclength_pt = self.normpath.arclength_pt() + self.arclength = unit.t_pt(self.arclength_pt) _axispos.__init__(self, convert) self.direction = direction @@ -1557,14 +1558,12 @@ class pathaxispos(_axispos): return None def vtickpoint_pt(self, v): - # XXX: path._at missing! - return [unit.topt(x) for x in self.normpath.at(self.normpath.lentopar(v * self.arclength))] + return self.normpath.at_pt(self.normpath.lentopar(v * self.arclength)) def vtickdirection(self, v): t = self.normpath.tangent(self.normpath.lentopar(v * self.arclength)) - # XXX: path._begin and path._end missing! - tbegin = [unit.topt(x) for x in t.begin()] - tend = [unit.topt(x) for x in t.end()] + tbegin = t.begin_pt() + tend = t.end_pt() dx = tend[0]-tbegin[0] dy = tend[1]-tbegin[1] norm = math.sqrt(dx*dx + dy*dy) @@ -2250,7 +2249,7 @@ class _axis: self.min = min if not self.fixmax and max is not None and (self.max is None or max > self.max): self.max = max - if None not in (self.min, self.max): + if None not in (self.min, self.max) and self.min != self.max: self.canconvert = 1 if self.reverse: self.setbasepoints(((self.min, 1), (self.max, 0))) @@ -3766,7 +3765,7 @@ class symbolline(_style): if data.errorbarattrs is not None: # TODO: bbox shortcut errorbarcanvas = graph.insert(canvas.canvas()) - data.line = path.path() + data.path = path.path() linebasepoints = [] lastvpos = None errorlist = [] @@ -3879,13 +3878,13 @@ class symbolline(_style): lastvpos = None if not validvpos: - # add baselinepoints to data.line + # add baselinepoints to data.path if len(linebasepoints) > 1: - data.line.append(path.moveto_pt(*linebasepoints[0])) + data.path.append(path.moveto_pt(*linebasepoints[0])) if len(linebasepoints) > 2: - data.line.append(path.multilineto_pt(linebasepoints[1:])) + data.path.append(path.multilineto_pt(linebasepoints[1:])) else: - data.line.append(path.lineto_pt(*linebasepoints[1])) + data.path.append(path.lineto_pt(*linebasepoints[1])) linebasepoints = [] # errorbar loop over the different direction having errorbars @@ -3966,17 +3965,17 @@ class symbolline(_style): if len(errorpath.path): errorbarcanvas.stroke(errorpath, data.errorbarattrs) - # add baselinepoints to data.line + # add baselinepoints to data.path if len(linebasepoints) > 1: - data.line.append(path.moveto_pt(*linebasepoints[0])) + data.path.append(path.moveto_pt(*linebasepoints[0])) if len(linebasepoints) > 2: - data.line.append(path.multilineto_pt(linebasepoints[1:])) + data.path.append(path.multilineto_pt(linebasepoints[1:])) else: - data.line.append(path.lineto_pt(*linebasepoints[1])) + data.path.append(path.lineto_pt(*linebasepoints[1])) - # stroke data.line + # stroke data.path if data.lineattrs is not None: - linecanvas.stroke(data.line, data.lineattrs) + linecanvas.stroke(data.path, data.lineattrs) def key_pt(self, c, x_pt, y_pt, width_pt, height_pt, data): self.drawsymbol_pt(c, x_pt+0.5*width_pt, y_pt+0.5*height_pt, data) @@ -4212,10 +4211,13 @@ class bar(_style): data.frompathattrs = None else: data.frompathattrs = self.defaultfrompathattrs + self.frompathattrs - if self.barattrs is not None: - data.barattrs = attr.selectattrs(self.defaultbarattrs + self.barattrs, selectindex, selecttotal) + if selecttotal > 1: + if self.barattrs is not None: + data.barattrs = attr.selectattrs(self.defaultbarattrs + self.barattrs, selectindex, selecttotal) + else: + data.barattrs = None else: - data.barattrs = None + data.barattrs = self.defaultbarattrs + self.barattrs data.selectindex = selectindex data.selecttotal = selecttotal if data.selecttotal != 1 and self.subnames is not None: diff --git a/test/functional/test_graph.py b/test/functional/test_graph.py index 924e6de2..2f5e03f2 100755 --- a/test/functional/test_graph.py +++ b/test/functional/test_graph.py @@ -57,10 +57,10 @@ def test_ownmark(c, x, y): g.plot(graph.data(data.data([[-1, 1], [5, 2], [11, 5], [5, 11], [4, -1]]), x=0, y=1), graph.line(lineattrs=[color.rgb.red])) g.finish() - p1=line1.line - p2=line2.line.reversed() - p3=line3.line.reversed() - p4=line4.line + p1=line1.path + p2=line2.path.reversed() + p3=line3.path.reversed() + p4=line4.path (seg1a,), (seg2a,) = p1.intersect(p2) (seg2b,), (seg3b,) = p2.intersect(p3) (seg3c,), (seg4c,) = p3.intersect(p4) -- 2.11.4.GIT