[PyX-user] rotated graphxy

 [PyX-user] rotated graphxy From: Dani Marti - 2006-01-11 20:10:11 ```Hi all, I'd like to insert two graphs on the same canvas. One is a normal xy graph, and the other is a histogram of the y values. My problem is, how can I insert the histogram rotated 90 degrees clockwise, at the right of the xy graph? In short, is it possible to rotate a graph? The idea I have in mind is to stress the meaning of a histogram as a 'projection' of the number of occurences on the y axis. from pyx import * from random import gauss from scipy import arange, transpose, array, zeros, randn from scipy.stats.stats import histogram c = canvas.canvas() # The first graph # --------------- g = graph.graphxy(width=8, height = 5) mean = 1.0 stddev = 0.2 data = zeros(1000) data = data + mean + stddev * randn(1000) # Maybe this is not necessary, but I can't think of # any easier way data_list = [[i,m] for i,m in enumerate(list(data))] g.plot(graph.data.list(data_list, x=1, y=2)) c.insert(g) # The second graph: a histogram # ----------------------------- # This is probably an awkward way to generate histograms. # It doesn't work, but this is not the matter here. n_bins = 50 histo, bin_min, bin_width, pts_outside = histogram(data[:], numbins=n_bins) x_histo = scipy.arange(bin_min + 0.5 * bin_width, step = bin_width, stop = bin_min + n_bins * bin_width) data_histo = transpose(array([x_histo, histo])) h = graph.graphxy(width = 5, height = 2) h.plot(graph.data.list(data_histo, x=1, y=2), [graph.style.histogram()]) ... And now, I'd like to rotate graph 'h' before inserting it at the right of graph g. I'm sorry if this question does not make any sense. Thanks a lot, guys PS Any advice about how to plot the histogram in this example will also be greatly appreciated. ```

 [PyX-user] rotated graphxy From: Dani Marti - 2006-01-11 20:10:11 ```Hi all, I'd like to insert two graphs on the same canvas. One is a normal xy graph, and the other is a histogram of the y values. My problem is, how can I insert the histogram rotated 90 degrees clockwise, at the right of the xy graph? In short, is it possible to rotate a graph? The idea I have in mind is to stress the meaning of a histogram as a 'projection' of the number of occurences on the y axis. from pyx import * from random import gauss from scipy import arange, transpose, array, zeros, randn from scipy.stats.stats import histogram c = canvas.canvas() # The first graph # --------------- g = graph.graphxy(width=8, height = 5) mean = 1.0 stddev = 0.2 data = zeros(1000) data = data + mean + stddev * randn(1000) # Maybe this is not necessary, but I can't think of # any easier way data_list = [[i,m] for i,m in enumerate(list(data))] g.plot(graph.data.list(data_list, x=1, y=2)) c.insert(g) # The second graph: a histogram # ----------------------------- # This is probably an awkward way to generate histograms. # It doesn't work, but this is not the matter here. n_bins = 50 histo, bin_min, bin_width, pts_outside = histogram(data[:], numbins=n_bins) x_histo = scipy.arange(bin_min + 0.5 * bin_width, step = bin_width, stop = bin_min + n_bins * bin_width) data_histo = transpose(array([x_histo, histo])) h = graph.graphxy(width = 5, height = 2) h.plot(graph.data.list(data_histo, x=1, y=2), [graph.style.histogram()]) ... And now, I'd like to rotate graph 'h' before inserting it at the right of graph g. I'm sorry if this question does not make any sense. Thanks a lot, guys PS Any advice about how to plot the histogram in this example will also be greatly appreciated. ```
 Re: [PyX-user] rotated graphxy From: Andre Wobst - 2006-01-11 22:01:36 ```Hi Dani, On 11.01.06, Dani Marti wrote: > I'd like to insert two graphs on the same canvas. One is a normal xy > graph, and the other is a histogram of the y values. My problem is, how > can I insert the histogram rotated 90 degrees clockwise, at the right of > the xy graph? In short, is it possible to rotate a graph? Sure, when you insert an object into another one you can apply a transformation (or even several transformations). But you don't need that as far as I understand what you want to do ... see the example below. > The idea I have in mind is to stress the meaning of a histogram as a > 'projection' of the number of occurences on the y axis. Right. This is something which I have in mind too. But this is something we should implement by a special data source (maybe a data source which use the histogram style as the default style). It could even be named histogram ... in graph.data ... we only need to think about the parametrization (i.e. the constructor arguments). The rest is almost trivial. Ok, let's do some coding ... from random import random from pyx import * _min, _max = min, max c = canvas.canvas() # note that an insert returns the inserted object ... g = c.insert(graph.graphxy(width=8)) # note that graph.data.list adds a line number at index 0 by default ... g.plot(graph.data.list([[random()] for i in range(100)], x=0, y=1), [graph.style.line()]) # you can apply (a) transformation(s) when inserting objects into other objects ... g = c.insert(graph.graphxy(width=8, xpos=10), [trafo.rotate(10, 10, 0)]) # we're creating automatic histograms out of tuples here ... g.plot(graph.data.list([[random(), (i+0.5)/10] for i in range(10)], x=1, y=2), [graph.style.histogram(autohistogramaxisindex=1)]) c.writeEPSfile("histogramfix") ################################################ # but there is a quite different, much more flexible way of specifying histograms ... g = graph.graphxy(width=8) # we prepare histogram data like for errorbars in one graph dimension. # The histogram style will take care of the rest ... and here you can # exchange the meaning of x and y just by the column names of your data ... randomlist = [random() for i in range(10)] randomlist.sort() d = graph.data.list([[min, max, random()] for min, max in zip(randomlist, randomlist[1:])], xmin=1, xmax=2, y=3) g.plot(d, [graph.style.histogram()]) g.writeEPSfile("histogramvary") ################################################ # make a simple histogram data source ... class histogramdata(graph.data._data): defaultstyles = [graph.style.histogram()] def __init__(self, l, title=None, min=None, max=None, sections=10, histogramaxis="x", valueaxis="y"): # well, the parametrisation is a problem, yes self.title = title if min is None: min = _min(*l) if max is None: max = _max(*l) self.columns = {"%smin" % histogramaxis: [min+(max-min)*i*1.0/sections for i in range(sections)], "%smax" % histogramaxis: [min+(max-min)*(i+1.0)/sections for i in range(sections)], valueaxis: [len([d for d in l if min+(max-min)*i*1.0/sections <= d < min+(max-min)*(i+1.0)/sections]) for i in range(sections)]} # then it all comes down to ... g = graph.graphxy(width=8) g.plot(histogramdata([random() for i in range(1000)])) g.writeEPSfile("histogramdata") # Simple and powerful, isn't it? 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/ ```