## [PyX-user] constant output size

 [PyX-user] constant output size From: trevorw - 2006-06-15 17:10:58 ```First, I should apologize for being a troll on this list, I post questions but no answers. Sorry. I'm still learning and thus am not really qualified to help in answering any questions yet. Thanks for your patience and help. Some time back I asked about plotting bar graphs with different colours. I managed to adapt the code and it is working nicely for me. I have two issues. Right now, my chart is being produced in a horizontal format and I'm using \rotatebox in LaTeX to orient it the way I want. I would prefer to produce with horizontal bars. Second, is that the number of bars on different graphs changes, and there are subtle variations in the size of the horizontal bars. This code is being used to produce a book in which some graphs will have only 2 or 3 bars and others will have 10 or 12. I need the bars and font sizes to remain constant. Any suggestions? This is my current code: # import data d = graph.data.list(habdata, title="habitat data", lineno=0, habitat=1, use=2, C=3, M=4, Y=5, K=6) # determine number of bars barcount=len(d.columns["C"]) barmax = max(d.columns["use"])*1.2 barwidth = barcount barheight = 5 # create data list tailored to meet this data set datalist = [(d.columns["habitat"][x],d.columns["use"][x],True, graph.style.bar([color.cmyk(d.columns["C"][x],d.columns["M"][x], d.columns["Y"][x], d.columns["K"][x]),]),barmax, '%d\%%' % (d.columns["use"][x])) for x in range(0, barcount)] # NOTE: must use escape character to keep percent sign # from being interpreted as a format string # prepare the graphbarcount*1.1 g = graph.graphxy(width=barwidth, height=barheight, x=graph.axis.bar(painter=graph.axis.painter.bar( nameattrs=[trafo.rotate(90),text.halign.right,text.size.Large], basepathattrs=None)), y=graph.axis.linear(painter=None), y2=None, x2=None) # plot it g.plot(graph.data.list(datalist, xname=1,y=2,multistyle=3,barstyle=4, stack=5,stacktext=6), [multistylebar(), graph.style.stackedbarpos("stack"), graph.style.bar([color.cmyk(0.0, 0.0, 0.0, 0.0),deco.stroked.clear]), graph.style.text("stacktext",textattrs=[trafo.rotate(90),text.halign.bo xright,text.size.Large])]) # write to a file outfile = string.join([graphdir, "/",string.lower(speciescode),"_habitats.eps"],'') g.writeEPSfile(outfile) FYI my multibar() class is as follows: from pyx import * # Define class that allows for bars with individual styles class multistylebar(graph.style.bar): defaultmultistylebarattrs = [color.palette.ReverseRainbow, deco.stroked([color.grey.black, style.linewidth.THICK])] def __init__(self, multistylebarattrs=[], **args): self.multistylebarattrs = multistylebarattrs graph.style.bar.__init__(self, **args) def columnnames(self, privatedata, sharedata, agraph, columnnames): if "multistyle" not in columnnames: raise ValueError("multistyle missing") if "barstyle" not in columnnames: raise ValueError("barstyle missing") return ["multistyle"] + ["barstyle"] + graph.style.bar.columnnames(self, privatedata, sharedata, agraph, columnnames) def selectstyle(self, privatedata, sharedata, agraph, selectindex, selecttotal): graph.style.bar.selectstyle(self, privatedata, sharedata, agraph, selectindex, selecttotal) privatedata.multistylebarattrs = attr.selectattrs(self.defaultmultistylebarattrs + self.multistylebarattrs, selectindex, selecttotal) def drawpoint(self, privatedata, sharedata, agraph, point): if point["multistyle"]: savebarattrs = privatedata.barattrs privatedata.barattrs = point["barstyle"].barattrs graph.style.bar.drawpoint(self, privatedata, sharedata, agraph, point) if point["multistyle"]: privatedata.barattrs = savebarattrs -- Trevor Wiens ```

 [PyX-user] constant output size From: trevorw - 2006-06-15 17:10:58 ```First, I should apologize for being a troll on this list, I post questions but no answers. Sorry. I'm still learning and thus am not really qualified to help in answering any questions yet. Thanks for your patience and help. Some time back I asked about plotting bar graphs with different colours. I managed to adapt the code and it is working nicely for me. I have two issues. Right now, my chart is being produced in a horizontal format and I'm using \rotatebox in LaTeX to orient it the way I want. I would prefer to produce with horizontal bars. Second, is that the number of bars on different graphs changes, and there are subtle variations in the size of the horizontal bars. This code is being used to produce a book in which some graphs will have only 2 or 3 bars and others will have 10 or 12. I need the bars and font sizes to remain constant. Any suggestions? This is my current code: # import data d = graph.data.list(habdata, title="habitat data", lineno=0, habitat=1, use=2, C=3, M=4, Y=5, K=6) # determine number of bars barcount=len(d.columns["C"]) barmax = max(d.columns["use"])*1.2 barwidth = barcount barheight = 5 # create data list tailored to meet this data set datalist = [(d.columns["habitat"][x],d.columns["use"][x],True, graph.style.bar([color.cmyk(d.columns["C"][x],d.columns["M"][x], d.columns["Y"][x], d.columns["K"][x]),]),barmax, '%d\%%' % (d.columns["use"][x])) for x in range(0, barcount)] # NOTE: must use escape character to keep percent sign # from being interpreted as a format string # prepare the graphbarcount*1.1 g = graph.graphxy(width=barwidth, height=barheight, x=graph.axis.bar(painter=graph.axis.painter.bar( nameattrs=[trafo.rotate(90),text.halign.right,text.size.Large], basepathattrs=None)), y=graph.axis.linear(painter=None), y2=None, x2=None) # plot it g.plot(graph.data.list(datalist, xname=1,y=2,multistyle=3,barstyle=4, stack=5,stacktext=6), [multistylebar(), graph.style.stackedbarpos("stack"), graph.style.bar([color.cmyk(0.0, 0.0, 0.0, 0.0),deco.stroked.clear]), graph.style.text("stacktext",textattrs=[trafo.rotate(90),text.halign.bo xright,text.size.Large])]) # write to a file outfile = string.join([graphdir, "/",string.lower(speciescode),"_habitats.eps"],'') g.writeEPSfile(outfile) FYI my multibar() class is as follows: from pyx import * # Define class that allows for bars with individual styles class multistylebar(graph.style.bar): defaultmultistylebarattrs = [color.palette.ReverseRainbow, deco.stroked([color.grey.black, style.linewidth.THICK])] def __init__(self, multistylebarattrs=[], **args): self.multistylebarattrs = multistylebarattrs graph.style.bar.__init__(self, **args) def columnnames(self, privatedata, sharedata, agraph, columnnames): if "multistyle" not in columnnames: raise ValueError("multistyle missing") if "barstyle" not in columnnames: raise ValueError("barstyle missing") return ["multistyle"] + ["barstyle"] + graph.style.bar.columnnames(self, privatedata, sharedata, agraph, columnnames) def selectstyle(self, privatedata, sharedata, agraph, selectindex, selecttotal): graph.style.bar.selectstyle(self, privatedata, sharedata, agraph, selectindex, selecttotal) privatedata.multistylebarattrs = attr.selectattrs(self.defaultmultistylebarattrs + self.multistylebarattrs, selectindex, selecttotal) def drawpoint(self, privatedata, sharedata, agraph, point): if point["multistyle"]: savebarattrs = privatedata.barattrs privatedata.barattrs = point["barstyle"].barattrs graph.style.bar.drawpoint(self, privatedata, sharedata, agraph, point) if point["multistyle"]: privatedata.barattrs = savebarattrs -- Trevor Wiens ```
 Re: [PyX-user] constant output size - additional comment From: trevorw - 2006-06-15 18:02:53 ```In addition to the other problems,when I have a graph where there are four columns of equal value, none of them are displayed. On Thu, 15 Jun 2006 11:11:07 -0600 trevorw wrote: > First, I should apologize for being a troll on this list, I post > questions but no answers. Sorry. I'm still learning and thus am not > really qualified to help in answering any questions yet. Thanks for your > patience and help. > > Some time back I asked about plotting bar graphs with different colours. > I managed to adapt the code and it is working nicely for me. > > I have two issues. Right now, my chart is being produced in a horizontal > format and I'm using \rotatebox in LaTeX to orient it the way I want. I > would prefer to produce with horizontal bars. > > Second, is that the number of bars on different graphs changes, and > there are subtle variations in the size of the horizontal bars. This > code is being used to produce a book in which some graphs will have only > 2 or 3 bars and others will have 10 or 12. I need the bars and font > sizes to remain constant. Any suggestions? > > This is my current code: > > # import data > d = graph.data.list(habdata, title="habitat data", lineno=0, > habitat=1, use=2, C=3, M=4, Y=5, K=6) > > # determine number of bars > barcount=len(d.columns["C"]) > > barmax = max(d.columns["use"])*1.2 > > barwidth = barcount > barheight = 5 > > # create data list tailored to meet this data set > datalist = [(d.columns["habitat"][x],d.columns["use"][x],True, > graph.style.bar([color.cmyk(d.columns["C"][x],d.columns["M"][x], > d.columns["Y"][x], > d.columns["K"][x]),]),barmax, '%d\%%' % > (d.columns["use"][x])) for x in range(0, barcount)] > # NOTE: must use escape character to keep percent sign > # from being interpreted as a format string > > # prepare the graphbarcount*1.1 > g = graph.graphxy(width=barwidth, height=barheight, > x=graph.axis.bar(painter=graph.axis.painter.bar( > nameattrs=[trafo.rotate(90),text.halign.right,text.size.Large], > basepathattrs=None)), y=graph.axis.linear(painter=None), > y2=None, x2=None) > # plot it > g.plot(graph.data.list(datalist, > xname=1,y=2,multistyle=3,barstyle=4, stack=5,stacktext=6), > [multistylebar(), > graph.style.stackedbarpos("stack"), > graph.style.bar([color.cmyk(0.0, 0.0, 0.0, > 0.0),deco.stroked.clear]), > graph.style.text("stacktext",textattrs=[trafo.rotate(90),text.halign.bo > xright,text.size.Large])]) > # write to a file > outfile = string.join([graphdir, > "/",string.lower(speciescode),"_habitats.eps"],'') > g.writeEPSfile(outfile) > > > FYI my multibar() class is as follows: > from pyx import * > > # Define class that allows for bars with individual styles > > class multistylebar(graph.style.bar): > > defaultmultistylebarattrs = [color.palette.ReverseRainbow, > deco.stroked([color.grey.black, style.linewidth.THICK])] > > def __init__(self, multistylebarattrs=[], **args): > self.multistylebarattrs = multistylebarattrs > graph.style.bar.__init__(self, **args) > > def columnnames(self, privatedata, sharedata, agraph, columnnames): > if "multistyle" not in columnnames: > raise ValueError("multistyle missing") > if "barstyle" not in columnnames: > raise ValueError("barstyle missing") > return ["multistyle"] + ["barstyle"] + > graph.style.bar.columnnames(self, privatedata, sharedata, agraph, > columnnames) > > def selectstyle(self, privatedata, sharedata, agraph, selectindex, > selecttotal): > graph.style.bar.selectstyle(self, privatedata, > sharedata, agraph, selectindex, selecttotal) > privatedata.multistylebarattrs = > attr.selectattrs(self.defaultmultistylebarattrs + > self.multistylebarattrs, selectindex, selecttotal) > > def drawpoint(self, privatedata, sharedata, agraph, point): > if point["multistyle"]: > savebarattrs = privatedata.barattrs > privatedata.barattrs = point["barstyle"].barattrs > graph.style.bar.drawpoint(self, privatedata, sharedata, agraph, > point) if point["multistyle"]: > privatedata.barattrs = savebarattrs > > -- > Trevor Wiens > > > _______________________________________________ > PyX-user mailing list > PyX-user@... > https://lists.sourceforge.net/lists/listinfo/pyx-user -- Trevor Wiens ```
 Re: [PyX-user] constant output size From: Alan G Isaac - 2006-06-15 21:28:55 ```On Thu, 15 Jun 2006, trevorw apparently wrote: > I would prefer to produce with horizontal bars. What goes wrong when you just reverse the roles of x and y and make the y axis a bar axis? > Second, is that the number of bars on different graphs changes, and > there are subtle variations in the size of the horizontal bars. This > code is being used to produce a book in which some graphs will have only > 2 or 3 bars and others will have 10 or 12. I need the bars and font > sizes to remain constant. Any suggestions? Are you saying you will allow the graph width to vary rather than allowing the bar width to vary? Or what? Cheers, Alan Isaac ```
 Re: [PyX-user] constant output size - additional comment From: Alan G Isaac - 2006-06-15 21:38:54 ```On Thu, 15 Jun 2006, trevorw apparently wrote: > In addition to the other problems,when I have a graph where there are > four columns of equal value, none of them are displayed. Is this the issue? http://pyx.sourceforge.net/examples/bargraphs/fromvalue.html hth, Alan Isaac ```
 Re: [PyX-user] constant output size - additional comment From: trevorw - 2006-06-15 22:21:15 ```On Thu, 15 Jun 2006 17:46:21 -0400 Alan G Isaac wrote: > On Thu, 15 Jun 2006, trevorw apparently wrote: > > In addition to the other problems,when I have a graph where there are > > four columns of equal value, none of them are displayed. > > Is this the issue? > http://pyx.sourceforge.net/examples/bargraphs/fromvalue.html > > hth, > Alan Isaac > Yes that was it. Thanks. T -- Trevor Wiens ```
 Re: [PyX-user] constant output size From: trevorw - 2006-06-15 22:23:48 ```On Thu, 15 Jun 2006 17:36:22 -0400 Alan G Isaac wrote: > On Thu, 15 Jun 2006, trevorw apparently wrote: > > I would prefer to produce with horizontal bars. > > What goes wrong when you just reverse the roles of x and y > and make the y axis a bar axis? > I didn't realize it was so easy. Done. Works fine. > > Second, is that the number of bars on different graphs changes, and > > there are subtle variations in the size of the horizontal bars. This > > code is being used to produce a book in which some graphs will have only > > 2 or 3 bars and others will have 10 or 12. I need the bars and font > > sizes to remain constant. Any suggestions? > > Are you saying you will allow the graph width to vary rather > than allowing the bar width to vary? Or what? > The number of bars will vary, but I want the fonts and the bar widths (technically heights because it is a horizontal graph) to be the same on each page, so that if I were to lay one graph with 8 bars and one graph with 4 bars against each other the top 4 bars would line up perfectly in both spacing and font size. Does that make sense? T -- Trevor Wiens ```
 Re: [PyX-user] constant output size - resolved From: trevorw - 2006-06-15 22:36:03 ```On Thu, 15 Jun 2006 16:23:58 -0600 trevorw wrote: > On Thu, 15 Jun 2006 17:36:22 -0400 > Alan G Isaac wrote: > > > On Thu, 15 Jun 2006, trevorw apparently wrote: > > > I would prefer to produce with horizontal bars. > > > > What goes wrong when you just reverse the roles of x and y > > and make the y axis a bar axis? > > > > I didn't realize it was so easy. Done. Works fine. > > > > Second, is that the number of bars on different graphs changes, and > > > there are subtle variations in the size of the horizontal bars. This > > > > code is being used to produce a book in which some graphs will have > only > > > 2 or 3 bars and others will have 10 or 12. I need the bars and font > > > sizes to remain constant. Any suggestions? > > > > Are you saying you will allow the graph width to vary rather > > than allowing the bar width to vary? Or what? > > > > The number of bars will vary, but I want the fonts and the bar widths > (technically heights because it is a horizontal graph) to be the same on > each page, so that if I were to lay one graph with 8 bars and one graph > with 4 bars against each other the top 4 bars would line up perfectly in > both spacing and font size. > Oops. The change was a function of the resize box in my latex document not the eps file created by pyx. Sorry. T -- Trevor Wiens ```
 Re: [PyX-user] constant output size - resolved From: Andre Wobst - 2006-06-16 05:24:19 ```Hi, On 15.06.06, trevorw wrote: > > > On Thu, 15 Jun 2006, trevorw apparently wrote: > > > What goes wrong when you just reverse the roles of x and y > > > and make the y axis a bar axis? > > > > > > > I didn't realize it was so easy. Done. Works fine. :-) > Oops. The change was a function of the resize box in my latex document > not the eps file created by pyx. I'm just reading the thread and was wondering about unexpected changes in the font *sizes*. But this seems solved now. BTW in case you want equal the size of the bars for different number of bars: For that you need to adjust the size of the graph yourself. As far as I've ever thought about graphs the size of a graph will never depend on the stuff plotted within the graph. This is a feature. The calculation of the size of the bars is done by a "size" variable on the involved axis data instances (bar axis and its subaxes). An easy solution to completely automate the graph size calculation for equal bar sizes could be to first create a graph for a given size along the bar axis. Then read out the size (in arbitrary units) after a g.dolayout() given by g.axes["..."].data.size and use this to create a second graph instance of the proper size. (Note that the bar axis size is independend of the graph size). While this sounds a bit complicated, I think this is a reasonable solution since it's a quite special problem to be solved. André -- by _ _ _ Dr. André Wobst / \ \ / ) wobsta@..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ ```
 Re: [PyX-user] constant output size - resolved From: trevorw - 2006-06-16 20:22:18 ```On Fri, 16 Jun 2006 07:24:12 +0200 Andre Wobst wrote: > Hi, > > On 15.06.06, trevorw wrote: > > > > On Thu, 15 Jun 2006, trevorw apparently wrote: > > > > What goes wrong when you just reverse the roles of x and y > > > > and make the y axis a bar axis? > > > > > > > > > > I didn't realize it was so easy. Done. Works fine. > > :-) > > > Oops. The change was a function of the resize box in my latex document > > not the eps file created by pyx. > > I'm just reading the thread and was wondering about unexpected changes > in the font *sizes*. But this seems solved now. BTW in case you want > equal the size of the bars for different number of bars: For that you > need to adjust the size of the graph yourself. As far as I've ever > thought about graphs the size of a graph will never depend on the > stuff plotted within the graph. This is a feature. The calculation of > the size of the bars is done by a "size" variable on the involved axis > data instances (bar axis and its subaxes). An easy solution to > completely automate the graph size calculation for equal bar sizes > could be to first create a graph for a given size along the bar axis. > Then read out the size (in arbitrary units) after a g.dolayout() given > by g.axes["..."].data.size and use this to create a second graph > instance of the proper size. (Note that the bar axis size is > independend of the graph size). While this sounds a bit complicated, I > think this is a reasonable solution since it's a quite special problem > to be solved. > What I've done which seems simpler to me is to define a width for each bar and simply multiply the number of bars by this value and then use this value to specify graph width (in my case height as I'm using a horizontal graph) and it appears to function fine. T -- Trevor Wiens ```
 Re: [PyX-user] constant output size - resolved From: Andre Wobst - 2006-06-23 17:35:52 ```Hi, On 16.06.06, trevorw wrote: > > I'm just reading the thread and was wondering about unexpected changes > > in the font *sizes*. But this seems solved now. BTW in case you want > > equal the size of the bars for different number of bars: For that you > > need to adjust the size of the graph yourself. As far as I've ever > > thought about graphs the size of a graph will never depend on the > > stuff plotted within the graph. This is a feature. The calculation of > > the size of the bars is done by a "size" variable on the involved axis > > data instances (bar axis and its subaxes). An easy solution to > > completely automate the graph size calculation for equal bar sizes > > could be to first create a graph for a given size along the bar axis. > > Then read out the size (in arbitrary units) after a g.dolayout() given > > by g.axes["..."].data.size and use this to create a second graph > > instance of the proper size. (Note that the bar axis size is > > independend of the graph size). While this sounds a bit complicated, I > > think this is a reasonable solution since it's a quite special problem > > to be solved. > > > > What I've done which seems simpler to me is to define a width for each > bar and simply multiply the number of bars by this value and then use > this value to specify graph width (in my case height as I'm using a > horizontal graph) and it appears to function fine. Sure. This is simpler, but it does not adjust the size automatically. And there are options to change the withs of the bars in a way, which you would need to recalculate manually as well. Think of the dist and firstdist and lastdist parameters. Anyway, I just thought about a fully automated solution and whether the current situation would not easily allow for creating such a solution. While I do not claim my suggestion is exactly beginner friendly, it can be implemented easily and it should do the job very well. There is not need to question the design of the graph system in general. (And that's what I'm really interested in to see, whether at some point we need certain redesigns, since those things need to be planed on the long run ...) André -- by _ _ _ Dr. André Wobst / \ \ / ) wobsta@..., http://www.wobsta.de/ / _ \ \/\/ / PyX - High quality PostScript and PDF figures (_/ \_)_/\_/ with Python & TeX: visit http://pyx.sourceforge.net/ ```