Help save net neutrality! Learn more.

## Re: [PyX-user] Retrieving color of plotitems

 Re: [PyX-user] Retrieving color of plotitems From: Axel Freyn - 2009-02-06 09:41:08 ```Hi Patrik, > I'm trying to get started making some more advanced plots with PyX. What I > want to do is create a plot with some lines on it, where each line is > surrounded by a half-transparent filled area (the 1-sigma deviation around > the line). (For an example see > http://governator.ucsc.edu/filer/p04tau100variance.png, which was made with > matplotlib.) > > The strategy I'm using is based on the "integral" example, ie: > > plot lines > retrieve paths from plotitems > create closed paths and fill them I'm not convinced that my strategy is the "best" or "official" - but at least it seems to work;-) from pyx import * g = graph.graphxy(width=8, x=graph.axis.linear(min=0, max=2), y=graph.axis.linear(min=0, max=2), key=graph.key.key(pos="br", dist=0.1)) g.plot([graph.data.function("x(y)=y**4", title=r"\$x = y^4\$"), graph.data.function("x(y)=y**2", title=r"\$x = y^2\$"), graph.data.function("x(y)=y", title=r"\$x = y\$"), graph.data.function("y(x)=x**2", title=r"\$y = x^2\$"), graph.data.function("y(x)=x**4", title=r"\$y = x^4\$")], [graph.style.line([color.gradient.Rainbow])]) g.finish() # Now, we search the properties of plotimes until we find the colours.. colors = {} for i in g.plotitems: for s in i.privatedatalist: if s.__dict__.has_key('lineattrs'): for c in s.__dict__['lineattrs']: if isinstance(c,pyx.color.color): colors[i.title] = c This wil create a dictionary, containg all title->color relations. Then there are 2 possibilites: a) you create the closed paths from parallel shifts of the original path. Then you can just use the graph as a canvas an continue plotting there (using the canvas-methods, no longer the plot-routines) b) you create the closed paths also by g.plot-Routines. Then, the easiest way is probably to create a new graphxy-Object, and plot everything there, as now you know already the colors But I'm sure, there are better ways to achive this result;-) Good Luck Axel ```

### Thread view

 [PyX-user] Retrieving color of plotitems From: Patrik Jonsson - 2009-02-06 01:05:10 Attachments: Message as HTML ```Hi, I'm trying to get started making some more advanced plots with PyX. What I want to do is create a plot with some lines on it, where each line is surrounded by a half-transparent filled area (the 1-sigma deviation around the line). (For an example see http://governator.ucsc.edu/filer/p04tau100variance.png, which was made with matplotlib.) The strategy I'm using is based on the "integral" example, ie: plot lines retrieve paths from plotitems create closed paths and fill them However, I'm running into some problems. The first is setting the color of the lines: I want the "main" lines to use a color gradient, and then I want to retrieve that color and use it again for the lines bounding the filled area. However, I can't figure out how to do this. The manual talks about various ways of setting the styles used for stroking the paths, but not how to retrieve them. I'm not even sure that this is part of the path derived from the plotitem, because it seems that the stroke style is set when passing the path to the canvas.stroke function and is not a property of the path itself? The second problem is that it's my understanding that to get the paths out of the plot, you need to finish it. But then you can't plot to it again, so at that point it's too late to add the shaded region? Any hints as to how to accomplish this would be much appreciated. cheers, /Patrik J. ```
 Re: [PyX-user] Retrieving color of plotitems From: André Wobst - 2009-02-06 08:38:33 ```Hi Patrik, Am 06.02.2009 um 01:40 schrieb Patrik Jonsson: > The first is setting the color of the lines: I want the "main" lines > to use a color gradient, and then I want to retrieve that color and > use it again for the lines bounding the filled area. However, I > can't figure out how to do this. The manual talks about various ways > of setting the styles used for stroking the paths, but not how to > retrieve them. I'm not even sure that this is part of the path > derived from the plotitem, because it seems that the stroke style is > set when passing the path to the canvas.stroke function and is not a > property of the path itself? Indeed, the stroke (and/or fill) attributes are not related to a path itself. (The decorated path is the combination of a path and its styles and other stuff (ornaments), but this is some internal detail.) To answer your question, the following code will return the color of the line in a graph: from pyx import * from pyx import attr g = graph.graphxy(width=10) pi = g.plot(graph.data.points([(0, 0), (1, 1)], x=1, y=2), [graph.style.line([color.rgb.red])]) g.dodata() c, = attr.getattrs(pi.lineattrs, [color.color]) g.writePDFfile("jonsson") Note that attr module is (currently) not imported by from pyx import * > The second problem is that it's my understanding that to get the > paths out of the plot, you need to finish it. But then you can't > plot to it again, so at that point it's too late to add the shaded > region? Indeed. IIRC this has changed in the current SVN head, but you would still have the problem, that the axes are not scaled including the additional stuff you want to draw. You could add the data for the shaded region within the graph too without stroking it immediately: pi2 = g.plot(graph.data.points([(0, 0.1), (1, 1.1)], x=1, y=2), [graph.style.line(None)]) Then you can just use pi2.path and stroke and fill it yourself. Beside that I would ask myself whether this plot style you want to create could not be realized similar to errorbars. I think this would lead to a very nice way of creating such plots. Best, André -- by _ _ _ Dr. André Wobst, Amselweg 22, 85716 Unterschleißheim / \ \ / ) wobsta@..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ ```
 Re: [PyX-user] Retrieving color of plotitems From: Axel Freyn - 2009-02-06 09:41:08 ```Hi Patrik, > I'm trying to get started making some more advanced plots with PyX. What I > want to do is create a plot with some lines on it, where each line is > surrounded by a half-transparent filled area (the 1-sigma deviation around > the line). (For an example see > http://governator.ucsc.edu/filer/p04tau100variance.png, which was made with > matplotlib.) > > The strategy I'm using is based on the "integral" example, ie: > > plot lines > retrieve paths from plotitems > create closed paths and fill them I'm not convinced that my strategy is the "best" or "official" - but at least it seems to work;-) from pyx import * g = graph.graphxy(width=8, x=graph.axis.linear(min=0, max=2), y=graph.axis.linear(min=0, max=2), key=graph.key.key(pos="br", dist=0.1)) g.plot([graph.data.function("x(y)=y**4", title=r"\$x = y^4\$"), graph.data.function("x(y)=y**2", title=r"\$x = y^2\$"), graph.data.function("x(y)=y", title=r"\$x = y\$"), graph.data.function("y(x)=x**2", title=r"\$y = x^2\$"), graph.data.function("y(x)=x**4", title=r"\$y = x^4\$")], [graph.style.line([color.gradient.Rainbow])]) g.finish() # Now, we search the properties of plotimes until we find the colours.. colors = {} for i in g.plotitems: for s in i.privatedatalist: if s.__dict__.has_key('lineattrs'): for c in s.__dict__['lineattrs']: if isinstance(c,pyx.color.color): colors[i.title] = c This wil create a dictionary, containg all title->color relations. Then there are 2 possibilites: a) you create the closed paths from parallel shifts of the original path. Then you can just use the graph as a canvas an continue plotting there (using the canvas-methods, no longer the plot-routines) b) you create the closed paths also by g.plot-Routines. Then, the easiest way is probably to create a new graphxy-Object, and plot everything there, as now you know already the colors But I'm sure, there are better ways to achive this result;-) Good Luck Axel ```
 Re: [PyX-user] Retrieving color of plotitems From: Michael SCHINDLER - 2009-02-06 10:05:57 ```Salut Patrik, On 05.02.09, Patrik Jonsson wrote: > The strategy I'm using is based on the "integral" example, ie: > > plot lines > retrieve paths from plotitems > create closed paths and fill them > However, I'm running into some problems. The first is setting the color of > the lines > The second problem is that it's my understanding that to get the paths out > of the plot, you need to finish it. But then you can't plot to it again, so > at that point it's too late to add the shaded region? Given that you want to have complete control over the colours, I would suggest a different "strategy": 1. Define the colours of the main and secondary lines 2. In a loop draw these lines (also the secondary) with the colours and styles specified explicitly. 3. Use graph.graphxy.finish() and retrieve the paths from plotitems 4. Draw the closed paths on a second canvas 5. Insert the graph into that canvas (make sure that the background is transparent. That solves both of your problems, I think. Cordialement, Michael ```