## Re: [Matplotlib-users] barh Chart - Not lining up

 Re: [Matplotlib-users] barh Chart - Not lining up From: John Hunter - 2006-08-03 21:39:27 ```>>>>> "Gregory" =3D=3D Gregory Pi=F1ero writes: Gregory> On 8/3/06, John Hunter Gregory> wrote: >> >>>>> "Gregory" =3D=3D Gregory Pi=F1ero >> writes: Gregory> bar centers on the y axis p1 =3D figure.gca().barh(pos,val) Gregory> figure.gca().set_yticks(pos) Gregory> figure.gca().set_yticklabels(('Tom', 'Dick', 'Harry', Gregory> 'Slim', 'Jim')) figure.gca().set_xlabel('Perfomance') Gregory> figure.gca().set_title('How fast do you want to go Gregory> today?') canvas=3DFigureCanvasAgg(figure) return canvas >> Gregory> I changed the code to above but it still gives a similair Gregory> result. The following, with svn mpl, works. If it doesn't work for you, make sure to upgrade your matplotlib; note the use of "align" which is a fairly recent addition to mpl val =3D [2,5,3,6,3] # the bar lengths pos =3D arange(5)+.5 # the bar centers on the y axis =20 p1 =3D figure.gca().barh(pos,val, align=3D'center') ax =3D figure.gca() ax.set_yticks(pos) ax.set_yticklabels(('Tom', 'Dick', 'Harry', 'Slim', 'Jim')) ax.set_xlabel('Perfomance') ax.set_title('How fast do you want to go today?') canvas=3DFigureCanvasAgg(figure) JDH ```

 [Matplotlib-users] barh Chart - Not lining up From: - 2006-08-03 21:10:19 Attachments: test.png ```Hi guys, I can't figure out why my code below is generating the attached image. What am I doing wrong? I tried to copy http://matplotlib.sourceforge.net/examples/barh_demo.py but also convert it so I can use the AGG backend. Any help is appriciated. Greg Pinero from __future__ import division import cgi,cgitb import os,sys import datetime import tempfile import matplotlib matplotlib.use('Agg') # force the antigrain backend from matplotlib.dates import YearLocator, MonthLocator, WeekdayLocator, DateFormatter, date2num from matplotlib import rc from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure from matplotlib.cbook import iterable import matplotlib.numerix as nx from Numeric import arange #makes arrays def start_chart(height,width): """Handles the parts of a chart that are common to all charts. """ assert height>0 and width>0 fig = Figure(figsize=(width,height)) #add an axes at left, bottom, width, height; (in fractions of figure size) ax = fig.add_axes([0.2, 0.3, 0.7, 0.6]) return fig def make_sideways_bar_chart_proto(figure): """ """ val = [2,5,3,6,3] # the bar lengths pos = arange(5)+.5 # the bar centers on the y axis p1 = figure.gca().barh(pos,val) figure.gca().set_yticklabels(('Tom', 'Dick', 'Harry', 'Slim', 'Jim')) figure.gca().set_xlabel('Perfomance') figure.gca().set_title('How fast do you want to go today?') canvas=FigureCanvasAgg(figure) return canvas def test_routine(): height,width=3,4 figure=start_chart(float(height),float(width)) canvas=make_sideways_bar_chart_proto(figure) canvas.print_figure('test.png', dpi=150) if __name__=='__main__': test_routine() ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: John Hunter - 2006-08-03 21:13:56 ```>>>>> "Gregory" =3D=3D Gregory Pi=F1ero writes: Gregory> bar centers on the y axis p1 =3D figure.gca().barh(pos,val) Gregory> figure.gca().set_yticklabels(('Tom', 'Dick', 'Harry', Gregory> 'Slim', 'Jim')) figure.gca().set_xlabel('Perfomance') Gregory> figure.gca().set_title('How fast do you want to go Gregory> today?') canvas=3DFigureCanvasAgg(figure) return canvas In the example you are following, the code reads yticks(pos, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')) This sets the ytick locations and labels. You are only setting the locations, and so you are incorrectly placing a tick and label at 0. You want ax.set_yticks(pos) ax.set_yticklabels(('Tom', 'Dick', 'Harry', 'Slim', 'Jim')) JDH ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: - 2006-08-03 21:27:44 Attachments: test.png ```On 8/3/06, John Hunter wrote: > >>>>> "Gregory" =3D=3D Gregory Pi=F1ero writes: > Gregory> bar centers on the y axis p1 =3D figure.gca().barh(pos,val) figure.gca().set_yticks(pos) > Gregory> figure.gca().set_yticklabels(('Tom', 'Dick', 'Harry', > Gregory> 'Slim', 'Jim')) figure.gca().set_xlabel('Perfomance') > Gregory> figure.gca().set_title('How fast do you want to go > Gregory> today?') canvas=3DFigureCanvasAgg(figure) return canvas > I changed the code to above but it still gives a similair result. ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: John Hunter - 2006-08-03 21:39:27 ```>>>>> "Gregory" =3D=3D Gregory Pi=F1ero writes: Gregory> On 8/3/06, John Hunter Gregory> wrote: >> >>>>> "Gregory" =3D=3D Gregory Pi=F1ero >> writes: Gregory> bar centers on the y axis p1 =3D figure.gca().barh(pos,val) Gregory> figure.gca().set_yticks(pos) Gregory> figure.gca().set_yticklabels(('Tom', 'Dick', 'Harry', Gregory> 'Slim', 'Jim')) figure.gca().set_xlabel('Perfomance') Gregory> figure.gca().set_title('How fast do you want to go Gregory> today?') canvas=3DFigureCanvasAgg(figure) return canvas >> Gregory> I changed the code to above but it still gives a similair Gregory> result. The following, with svn mpl, works. If it doesn't work for you, make sure to upgrade your matplotlib; note the use of "align" which is a fairly recent addition to mpl val =3D [2,5,3,6,3] # the bar lengths pos =3D arange(5)+.5 # the bar centers on the y axis =20 p1 =3D figure.gca().barh(pos,val, align=3D'center') ax =3D figure.gca() ax.set_yticks(pos) ax.set_yticklabels(('Tom', 'Dick', 'Harry', 'Slim', 'Jim')) ax.set_xlabel('Perfomance') ax.set_title('How fast do you want to go today?') canvas=3DFigureCanvasAgg(figure) JDH ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: - 2006-08-03 21:45:02 ```On 8/3/06, John Hunter wrote: > The following, with svn mpl, works. If it doesn't work for you, make > sure to upgrade your matplotlib; note the use of "align" which is a > fairly recent addition to mpl > > val = [2,5,3,6,3] # the bar lengths > pos = arange(5)+.5 # the bar centers on the y axis > > p1 = figure.gca().barh(pos,val, align='center') > ax = figure.gca() > ax.set_yticks(pos) > ax.set_yticklabels(('Tom', 'Dick', 'Harry', 'Slim', 'Jim')) > ax.set_xlabel('Perfomance') > ax.set_title('How fast do you want to go today?') > canvas=FigureCanvasAgg(figure) So all you changed was to add in align='center' ? I assume me using figure.gca() is equivalent to your use of ax? Is it hard to upgrade to the latest version from svn? Any directions? -Greg ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: PGM - 2006-08-03 22:21:02 ```On Thursday 03 August 2006 17:44, Gregory Pi=F1ero wrote: > So all you changed was to add in align=3D'center' ? I assume me using > figure.gca() is equivalent to your use of ax? Yes, gca() is "get the current axes object". > Is it hard to upgrade to the latest version from svn? Any directions? [From=20 'http://projects.scipy.org/neuroimaging/ni/wiki/DevelopmentInstallFedora';] svn co https://svn.sourceforge.net/svnroot/matplotlib/trunk/matplotlib=20 matplotlib-trunk cd matplotlib-trunk python setup.py build sudo python setup.py install cd .. But Greg, if it's only for that, try to put an empty label in front of the= =20 others: figure.gca().set_yticklabels(('', 'Tom', 'Dick', 'Harry', 'Slim', 'Jim')) That might do the trick ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: - 2006-08-04 01:26:09 ```On 8/3/06, PGM wrote: > But Greg, if it's only for that, try to put an empty label in front of the > others: > figure.gca().set_yticklabels(('', 'Tom', 'Dick', 'Harry', 'Slim', 'Jim')) > That might do the trick No, that didn't work. Did the barh just not work well in my version? ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: - 2006-08-04 00:41:44 ```Another question, why are there only four bars showing up when I have 5 values and 5 labels? ```
 Re: [Matplotlib-users] barh Chart - Not lining up From: PGM - 2006-08-04 01:11:33 ```On Thursday 03 August 2006 20:41, Gregory Pi=F1ero wrote: > Another question, why are there only four bars showing up when I have > 5 values and 5 labels? ??? On my machine, (matplotlib.__version__ =3D '0.87.4'), the script you posted= =20 around 5PM (EST) works OK, five bars, five labels nicely placed (once=20 prepended with a fake one )... ```