matplotlib-users

 [Matplotlib-users] Getting crazy with "legend" From: Eric Emsellem - 2005-04-27 14:06:57 ```Hi, I found an odd problem with "legend". I get the right labels/text but not the "small colour bars" associated to them. There is something I must do wrong.. Here is a fake example: =========== pa1 = hist(arange(1,100,1), bins=arange(0,100,10)) pa2 = hist(arange(1,100,1), bins=arange(0,100,10)) clf width = 5 # the width of the bars p1 = bar(pa1[1], pa1[0], width, color='r') p2 = bar(pa2[1]+width, pa2[0], width, color='y') legend([pa1[0], pa2[0]], ["\$r_e/5\$", "\$r_e/2.5\$"], shadow=True) ============ Thanks for any input on this! Eric -- =============================================================== Observatoire de Lyon emsellem@... 9 av. Charles-Andre tel: +33 4 78 86 83 84 69561 Saint-Genis Laval Cedex fax: +33 4 78 86 83 86 France http://www-obs.univ-lyon1.fr/eric.emsellem =============================================================== ```
 Re: [Matplotlib-users] Getting crazy with "legend" From: John Hunter - 2005-04-27 14:59:48 ```>>>>> "Eric" == Eric Emsellem writes: Eric> Hi, I found an odd problem with "legend". I get the right Eric> labels/text but not the "small colour bars" associated to Eric> them. There is something I must do wrong.. Eric> Here is a fake example: Eric> Thanks for any input on this! There are two problems with your example pa1 = hist(arange(1,100,1), bins=arange(0,100,10)) pa2 = hist(arange(1,100,1), bins=arange(0,100,10)) clf() matplotlib provides a "no plot" hist function in matplotlib.mlab. So rather than calling the pylab hist (which plots) and then clearing the figure with clf (you forgot the parentheses of clf, btw), call matplotlib.mlab.hist which computes the histogram but doesn't plot. width = 5 # the width of the bars p1 = bar(pa1[1], pa1[0], width, color='r') p2 = bar(pa2[1]+width, pa2[0], width, color='y') The above code is OK. legend([pa1[0], pa2[0]], ["\$r_e/5\$", "\$r_e/2.5\$"], shadow=True) This is the second problem. pa1 and pa2 in your example are the return values from pylab hist, which is (n, bins, patches). You need to pass the patches returned by the bar command, eg p1[0], p2[0] Here is the recommended way: from pylab import * import matplotlib.mlab # provides hist w/o plot n1, bins1 = matplotlib.mlab.hist(arange(1,100,1), bins=arange(0,100,10)) n2, bins1 = matplotlib.mlab.hist(arange(1,100,1), bins=arange(0,100,10)) width = 5 # the width of the bars p1 = bar(bins1, n1, width, color='r') p2 = bar(bins1+width, n2, width, color='y') legend([p1[0], p2[0]], ["\$r_e/5\$", "\$r_e/2.5\$"], shadow=True) show() Should help! JDH ```