dynamically setting the height of a barh plot

View: New views
1 Messages — Rating Filter:   Alert me  

dynamically setting the height of a barh plot

by nbv4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to create a dynamically generated png of an arbitrary number of barplots. The height of the png should correspond with the number of plots that are made. In jpGraph this is very easy. All you have to do is set the height of the image to top-margin + (number_of_bars * width_of_bars) + bottom_margin. But matplotlib doesn't use pixels, so it's not as easy. Here is my code:

def output(self):
        # titles = a list of the titles that get displayed down the left side
        # vals = a list of values that will determine how big to make each bar
        titles, vals = self.get_data()
       
        # set the height of each bar based on how many items there are
        from pylab import arange
        pos = arange(len(vals))
       
        rects = self.ax.barh(pos, vals,
                     height=0.8,   # this doesn't seem to be in any sane unit
                     align='center',
                     color=self.bar_color)
       
        from pylab import yticks, xticks
        yticks(pos, map(self.make_ytick, titles))
        xticks([], []) #remove everything from the x axis
       
        from pylab import title
        title(self.get_column_title() + " " + self.title())
       
        self.annotate_bars(rects) # add the text to the end of each bar
       
        return self.fig

then that fig is outputted like this:

        response=HttpResponse(content_type='image/png')
       
        fig = self.output()
       
        # this is the magic formula that needs to be changed
        # self.len is the number of bars there are
        # 0.15 inches per bar + 1 inch for margins
        fig.set_figheight((.15 * self.len) +1)
       
        fig.savefig(response,
                    bbox_inches="tight",
                    pad_inches=0.5,      
                    format='png')
       
        return response

Here are some output examples of my code:

http://sites.google.com/site/nbvfour/home

As you can see, there are random swaths of whitespace at the top and bottom of each, and the bard are not consistent. Hoe can I fix this? Also, how do I get those ugly tick marks to go away wile still keeping the titles of each bar?