From: Guest2 C. <cer...@si...> - 2006-03-08 15:30:24
|
Hello PyX-Users, can anybody tell me how to use various user-specified colors for comparing bar graphs? (cf. the "compare" example at http://pyx.sourceforge.net/examples/bargraphs/index.html) PyX-0.8.1 wouldn't accept handing multiple colors as parameters to graph.style.bar(...). Inserting another graph.style.bar(...) command between both graph.data.file(...) commands was also refused. Here's the code with just one user-specified color for both bars. ---------- from pyx import * a =3D graph.axis.nestedbar(title=3D"x-axis") g =3D graph.graphxy(width=3D10, x=3Da, height=3DNone, y=3Dgraph.axis.linear(title=3D"y-axis", min=3D0, max=3D10)) g.plot([graph.data.file("data.txt", xname=3D0, y=3D2), graph.data.file("data.txt", xname=3D0, y=3D3)], [graph.style.bar([color.rgb(0.7, 0.3, 0.5)])] ) g.writePDFfile("bar.pdf") g.writeEPSfile("bar.eps") ---------- Any suggestions how to color both bars user-specifically? Regards, Arthur |
From: Andre W. <wo...@us...> - 2006-03-08 16:32:42
|
Hi Arthur, On 08.03.06, Guest2 CERT wrote: > g.plot([graph.data.file("data.txt", xname=0, y=2), > graph.data.file("data.txt", xname=0, y=3)], > [graph.style.bar([color.rgb(0.7, 0.3, 0.5)])] > ) You need to use an changeable attribute here. A palette will do, but you can also build a changable attribute from a list of colors. See 4.3.5 in the FAQ. HTH, André -- by _ _ _ Dr. André Wobst / \ \ / ) wo...@us..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ |
From: Alan G I. <ai...@am...> - 2006-03-08 21:39:56
|
> On 08.03.06, Guest2 CERT wrote: >> g.plot([graph.data.file("data.txt", xname=0, y=2), >> graph.data.file("data.txt", xname=0, y=3)], >> [graph.style.bar([color.rgb(0.7, 0.3, 0.5)])] >> ) On Wed, 8 Mar 2006, Andre Wobst apparently wrote: > You need to use an changeable attribute here. A palette will do, but > you can also build a changable attribute from a list of colors. See > 4.3.5 in the FAQ. I thought this meant I could do: mystyle = [pyx.graph.style.bar(barattrs=[pyx.attr.changelist([pyx.color.rgb.red, pyx.color.rgb.green])])] g.plot( pyx.graph.data.list(zip(Xnames,Yvals), xname=1, y=2), mystyle ) But I'm wrong. Why? I know I'm overlooking the obvious. (The example is just illustrative.) Thanks, Alan Isaac |
From: Alan G I. <ai...@am...> - 2006-03-09 04:28:13
|
OK, here's a different but possibly related question. I want one of my bars to have a different color than the rest. How best to do this? Thank you, Alan Isaac |
From: Andre W. <wo...@us...> - 2006-03-09 06:35:09
Attachments:
minimal.py
|
Hi Alan, On 08.03.06, Alan G Isaac wrote: > OK, here's a different but possibly related question. > I want one of my bars to have a different color than the rest. > How best to do this? Well, you could do a quick'n'dirty solution. (g.pos() might be of great help here.) But you can of course also do a real solution within the framework of PyX. You would need to create an specialized bar style, which can highlight some bars. See the enclosed example of what you'll need to do for that. While it's quite straight forward, I'm aware of the fact that you'll need to really go into the details on how graph styles work. Still, there are no mysteries at all ... André -- by _ _ _ Dr. André Wobst / \ \ / ) wo...@us..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ |
From: Andre W. <wo...@us...> - 2006-03-09 06:09:19
|
Hi, On 08.03.06, Alan G Isaac wrote: > I thought this meant I could do: > > mystyle = [pyx.graph.style.bar(barattrs=[pyx.attr.changelist([pyx.color.rgb.red, pyx.color.rgb.green])])] > g.plot( > pyx.graph.data.list(zip(Xnames,Yvals), xname=1, y=2), > mystyle > ) > > But I'm wrong. Why? > I know I'm overlooking the obvious. > (The example is just illustrative.) Yeah, but you need to use the style on several datasets (i.e. some data instances) to make sense out of the changeable attribute. You can either user several plot commands with the same style instances or use a singe plot instance and a list of datasets as the first parameter (instead of a single dataset as shown in your code). You can even mix that. Use several plot commands with lists of datasets. The styles will then be iterated over all datasets. André -- by _ _ _ Dr. André Wobst / \ \ / ) wo...@us..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ |
From: Alan G I. <ai...@am...> - 2006-03-11 05:07:37
|
On Thu, 9 Mar 2006, Andre Wobst apparently wrote: > But you can of course also do a real solution within the > framework of PyX. You would need to create an specialized > bar style, which can highlight some bars. > [...] there are no mysteries at all ... Well I'm still a newbie dealing with many mysteries here. But how about allowing provision of a styles list to the bar class? I suggest this is a good idea to include in the bar class (and not an unusual functionality). Without in any way presuming to suggest a design---I don't pretend to know a good one---something that would function like below. Cheers, Alan ============================================================================= class bar_styles(graph.style.bar): ''' Allow a styles list, that is cycled through. ''' default_style = [color.palette.ReverseRainbow, deco.stroked([color.grey.black])] def __init__(self, styles_list=[], **args): self.drawpoint_calls = 0 self.num_styles = len(styles_list) self.styles_list = styles_list graph.style.bar.__init__(self, **args) def selectstyle(self, privatedata, sharedata, agraph, selectindex, selecttotal): graph.style.bar.selectstyle(self, privatedata, sharedata, agraph, selectindex, selecttotal) privatedata.styles_list = [attr.selectattrs(self.default_style + mystyle, selectindex, selecttotal) for mystyle in self.styles_list] def drawpoint(self, privatedata, sharedata, agraph, point): savebarattrs = privatedata.barattrs privatedata.barattrs = privatedata.styles_list[self.drawpoint_calls % self.num_styles] graph.style.bar.drawpoint(self, privatedata, sharedata, agraph, point) privatedata.barattrs = savebarattrs self.drawpoint_calls += 1 g = graph.graphxy(width=8, x=graph.axis.bar()) style1 = [pyx.color.rgb.red, deco.stroked([color.grey.black, style.linewidth.THICK])] style2 = [pyx.color.rgb.blue, deco.stroked([color.grey.black])] g.plot(graph.data.list([["a", 1], ["b", 5], ["c", 9]], xname=1, y=2), [bar_styles([style1]*2+[style2])]) g.writeEPSfile("c:\\temp.eps") |
From: Andre W. <wo...@us...> - 2006-03-14 13:29:12
|
Hi Alan, On 11.03.06, Alan G Isaac wrote: > Well I'm still a newbie dealing with many mysteries here. > But how about allowing provision of a styles list to the bar > class? I suggest this is a good idea to include in the bar > class (and not an unusual functionality). Without in any > way presuming to suggest a design---I don't pretend to know > a good one---something that would function like below. You're right. We need such a style. It's also quite clear how it should work, although I do have a different use case in mind, namely a pie diagram, or -- speaking in terms of a xygraph -- something which fills up to a -- say -- 100%. The feature of such a style would be to cycle thru a set of changeable (stroke/fill) styles when accessing the dataset. In the end we could use this style for what you want to do or for what I do have in mind just by changing the (implicit) positioning style. > ============================================================================= > > class bar_styles(graph.style.bar): > ''' > Allow a styles list, that is cycled through. > ''' > > default_style = [color.palette.ReverseRainbow, > deco.stroked([color.grey.black])] > > def __init__(self, styles_list=[], **args): > self.drawpoint_calls = 0 > self.num_styles = len(styles_list) > self.styles_list = styles_list > graph.style.bar.__init__(self, **args) > > def selectstyle(self, privatedata, sharedata, agraph, > selectindex, selecttotal): > graph.style.bar.selectstyle(self, > privatedata, sharedata, > agraph, > selectindex, selecttotal) > privatedata.styles_list = [attr.selectattrs(self.default_style > + mystyle, selectindex, selecttotal) > for mystyle in self.styles_list] > > def drawpoint(self, privatedata, sharedata, agraph, point): > savebarattrs = privatedata.barattrs > privatedata.barattrs = privatedata.styles_list[self.drawpoint_calls % self.num_styles] > graph.style.bar.drawpoint(self, privatedata, sharedata, agraph, point) > privatedata.barattrs = savebarattrs > self.drawpoint_calls += 1 > > > g = graph.graphxy(width=8, x=graph.axis.bar()) > style1 = [pyx.color.rgb.red, deco.stroked([color.grey.black, style.linewidth.THICK])] > style2 = [pyx.color.rgb.blue, deco.stroked([color.grey.black])] > g.plot(graph.data.list([["a", 1], ["b", 5], ["c", 9]], xname=1, y=2), > [bar_styles([style1]*2+[style2])]) > g.writeEPSfile("c:\\temp.eps") Just some remarks to your code: * drawpoint_calls should be initialized in initdrawpoints and should be saved in privatedata (thus the data handling becomes local to the graph, not local to the style -- try to use the same style instance several times and you'll see what I mean) * we should get rid of num_styles (the numbering should be driven by the data, not by the styles) * we should use changeable attributes, such that for example a palette could be used to cycle thru colors * you do not need to keep the barattrs (hence you can remove the savebarattrs) ... the trick of setting the barattrs is just a quick'n'dirty injection of some modifying attributes to reuse all the other stuff from the bar-style code ... André -- by _ _ _ Dr. André Wobst / \ \ / ) wo...@us..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ |
From: Alan G I. <ai...@am...> - 2006-03-10 13:22:35
|
> On 08.03.06, Alan G Isaac wrote: >> I thought this meant I could do: >> mystyle = [pyx.graph.style.bar(barattrs=[pyx.attr.changelist([pyx.color.rgb.red, pyx.color.rgb.green])])] >> g.plot( >> pyx.graph.data.list(zip(Xnames,Yvals), xname=1, y=2), >> mystyle >> ) On Thu, 9 Mar 2006, Andre Wobst apparently wrote: > Yeah, but you need to use the style on several datasets > (i.e. some data instances) to make sense out of the > changeable attribute. Sure. But I cannot even get the first instance to work. See below. Thanks, Alan ############# script ################################ import pyx print "PyX version: ", pyx.__version__ g = pyx.graph.graphxy(width=8, x=pyx.graph.axis.bar(), y=pyx.graph.axis.linear(min=0, max=10, parter=None, manualticks=[pyx.graph.axis.tick.tick(10*xi/5) for xi in range(6)])) # this works fine: mystyle = pyx.graph.style.bar(barattrs=[pyx.color.rgb.green]) # this works not at all: mystyle = pyx.graph.style.bar(barattrs=[pyx.attr.changelist([pyx.color.rgb.red, pyx.color.rgb.green])]) g.plot(pyx.graph.data.list(zip(["two","four","six"],[2,4,6]),xname=1,y=2), [mystyle]) g.writeEPSfile("c:\\temp\\temp.eps") ############# script output ######################## PyX version: 0.8.1 Traceback (most recent call last): File "<stdin>", line 1, in ? File "c:\temp.py", line 15, in ? g.writeEPSfile("c:\\temp\\temp.eps") File "C:\Python24\lib\site-packages\pyx\canvas.py", line 323, in wrappedindocu ment return method(d, filename) File "C:\Python24\lib\site-packages\pyx\document.py", line 138, in writeEPSfil e pswriter.epswriter(self, filename, *args, **kwargs) File "C:\Python24\lib\site-packages\pyx\pswriter.py", line 235, in __init__ bbox = page.bbox() File "C:\Python24\lib\site-packages\pyx\document.py", line 78, in bbox bbox = self.canvas.bbox() File "C:\Python24\lib\site-packages\pyx\graph\graph.py", line 445, in bbox self.finish() File "C:\Python24\lib\site-packages\pyx\graph\graph.py", line 342, in finish self.domethods[0]() File "C:\Python24\lib\site-packages\pyx\graph\graph.py", line 320, in dodata plotitem.draw(self) File "C:\Python24\lib\site-packages\pyx\graph\graph.py", line 107, in draw style.drawpoint(privatedata, self.sharedata, graph, point) File "C:\Python24\lib\site-packages\pyx\graph\style.py", line 1430, in drawpoi nt privatedata.rectcanvas.fill(p, privatedata.barattrs) File "C:\Python24\lib\site-packages\pyx\canvas.py", line 290, in fill self.draw(path, [deco.filled]+list(attrs)) File "C:\Python24\lib\site-packages\pyx\canvas.py", line 253, in draw attrs = attr.mergeattrs(attrs) File "C:\Python24\lib\site-packages\pyx\attr.py", line 60, in mergeattrs raise TypeError("only instances of class attr.attr are allowed") TypeError: only instances of class attr.attr are allowed >>> |
From: Andre W. <wo...@us...> - 2006-03-10 13:45:36
|
Hi Alan, On 10.03.06, Alan G Isaac wrote: > Sure. But I cannot even get the first instance to work. I'm sorry, this is a bug. (It has its origin in the way we did bar graphs in older releases, where a bar needed to make the nesting and the stacking all at the same time.) Fixed it in changeset 2576. However, without nesting of bar axes and styles, you don't need to use changeable attributes and than the problem doesn't occur. And when using the bar style multiple times the proper select is performed at PyX 0.8.1 as well, such that with the following changes everything should work as expected. > ############# script ################################ > import pyx > print "PyX version: ", pyx.__version__ # Cool. Like that. > g = pyx.graph.graphxy(width=8, > x=pyx.graph.axis.bar(), x=pyx.graph.axis.nestedbar(), > y=pyx.graph.axis.linear(min=0, > max=10, > parter=None, > manualticks=[pyx.graph.axis.tick.tick(10*xi/5) for xi in range(6)])) > # this works fine: > mystyle = pyx.graph.style.bar(barattrs=[pyx.color.rgb.green]) > # this works not at all: > mystyle = pyx.graph.style.bar(barattrs=[pyx.attr.changelist([pyx.color.rgb.red, pyx.color.rgb.green])]) > g.plot(pyx.graph.data.list(zip(["two","four","six"],[2,4,6]),xname=1,y=2), [mystyle]) g.plot([pyx.graph.data.list(zip(["two","four","six"],[2,4,6]),xname=1,y=2), pyx.graph.data.list(zip(["two","four","six"],[3,5,7]),xname=1,y=2)], [mystyle]) > g.writeEPSfile("c:\\temp\\temp.eps") HTH, André -- by _ _ _ Dr. André Wobst / \ \ / ) wo...@us..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ |
From: Alan G I. <ai...@am...> - 2006-03-10 15:38:34
|
On Fri, 10 Mar 2006, Andre Wobst apparently wrote: > with the following changes everything > should work as expected. Yes. Thanks, Alan |