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
   / \ \    / )   [EMAIL PROTECTED], http://www.wobsta.de/
  / _ \ \/\/ /    PyX - High quality PostScript and PDF figures
 (_/ \_)_/\_/     with Python & TeX: visit http://pyx.sourceforge.net/


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
PyX-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyx-user

Reply via email to