[Matplotlib-users] Legend in a multibars chart
Hi, I'm plotting two subplots using bar charts, the first one contains one dataset (one bar for each abscissa value), the second 3 dataset (3 bars for each abscissa value, each one with its own color). The legend of the multibars chart is not correct, it shows the color of the first dataset for all the bars legend. Note that it works correctly with matplotlib.pyplot.plot instead of matplotlib.pyplot.bar. Any idea to fix it? Thanks, A.Frances CODE OF MULTIBARS #--- # Name:multibars # Purpose: # # Author: alf # # Created: 05-01-2011 # Copyright: (c) alf 2010 # Licence: #--- #!/usr/bin/env python import matplotlib.pyplot as plt from numpy import array import random strTitle = 'Bars plot - multi data and legend' x = array(range(10)) y1 = [] y2 = [[],[],[]] for i in range(len(x)): y1.append(1.0 + random.random()) for j in range(len(y2)): y2[j].append(0.5 + random.random()) lbl_y1 = [] lbl_y1.append('bar 1') lbl_y2 = 'bar 2' lbl_type = ['red', 'green', 'blue'] colors_y2 = ([1,0,0],[0,1,0],[0,0,1]) lbls_y2 = [] for i in range(len(lbl_type)): lbls_y2.append(lbl_y2 + " " + lbl_type[i]) fig = plt.figure() ax1=fig.add_subplot(2,1,1) ax1.set_title("Single data") plt.bar(x, y1, color='purple', linewidth = 0, align='edge', width = 0.8) plt.legend(lbl_y1, loc= 0) plt.xticks(x) ax2=fig.add_subplot(2,1,2, sharex=ax1) ax2.set_title("Multi data") for i in range(len(y2)): plt.bar(x+(0.8*float(i)/len(y2)),y2[i],color = colors_y2[i], linewidth=0, align='edge', width = (0.8/len(y2))) plt.legend(lbls_y2, loc=0) plt.xticks(x) plt.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.95, wspace=0.1, hspace=0.15) plt.show() ## CODE OF MULTILINES #--- # Name:multilines # Purpose: # # Author: alf # # Created: 05-01-2011 # Copyright: (c) alf 2010 # Licence: #--- #!/usr/bin/env python import matplotlib.pyplot as plt from numpy import array import random strTitle = 'Lines plot - multi data and legend' x = array(range(10)) y1 = [] y2 = [[],[],[]] for i in range(len(x)): y1.append(1.0 + random.random()) for j in range(len(y2)): y2[j].append(0.5 + random.random()) lbl_y1 = [] lbl_y1.append('line 1') lbl_y2 = 'line 2' lbl_type = ['red', 'green', 'blue'] colors_y2 = ([1,0,0],[0,1,0],[0,0,1]) lbls_y2 = [] for i in range(len(lbl_type)): lbls_y2.append(lbl_y2 + " " + lbl_type[i]) fig = plt.figure() ax1=fig.add_subplot(2,1,1) ax1.set_title("Single data") plt.plot(x, y1, color='purple') plt.legend(lbl_y1, loc= 0) plt.xticks(x) ax2=fig.add_subplot(2,1,2, sharex=ax1) ax2.set_title("Multi data") for i in range(len(y2)): plt.plot(x,y2[i],color = colors_y2[i]) plt.legend(lbls_y2, loc=0) plt.xticks(x) plt.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.95, wspace=0.1, hspace=0.15) plt.show() ## -- Learn how Oracle Real Application Clusters (RAC) One Node allows customers to consolidate database storage, standardize their database environment, and, should the need arise, upgrade to a full multi-node Oracle RAC database without downtime or disruption http://p.sf.net/sfu/oracle-sfdevnl ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Legend in a multibars chart
Hi Ben, Thanks for your nicer and working code! It works perfectly and is indeed clean. Up to now I didn't noticed any problem with the positioning of the legend. Best regards, Alain From: ben.v.r...@gmail.com [mailto:ben.v.r...@gmail.com] On Behalf Of Benjamin Root Sent: Thursday, 06 January, 2011 04:43 To: Alain Pascal Frances Cc: matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] Legend in a multibars chart On Wed, Jan 5, 2011 at 7:19 AM, Alain Pascal Frances mailto:frances17...@itc.nl>> wrote: Hi, I'm plotting two subplots using bar charts, the first one contains one dataset (one bar for each abscissa value), the second 3 dataset (3 bars for each abscissa value, each one with its own color). The legend of the multibars chart is not correct, it shows the color of the first dataset for all the bars legend. Note that it works correctly with matplotlib.pyplot.plot instead of matplotlib.pyplot.bar. Any idea to fix it? Thanks, A.Frances I can confirm the problem, and I have a few suspects as to the cause. Most notably that the legend code probably assumes that it is looking for line objects, not patch objects and starts using its own color cycler when it can't get a list of colors to correspond with its list of labels. As a work-around, you can add a label keyword to the call to bar() and not bother giving legend() a list of labels. Here is a cleaned-up version of your first code. Note I also took a moment to take advantage of python syntax and better numpy/matplotlib coding styles. The only issue seems to be that there might be a bug with respect to positioning the legend: import matplotlib.pyplot as plt import numpy as np strTitle = 'Bars plot - multi data and legend' x = np.arange(10) y1 = 1.0 + np.random.random(x.shape) y2 = 0.5 + np.random.random((3, len(x))) lbl_y1 = 'bar 1' lbl_type = ['red', 'green', 'blue'] colors_y2 = ([1,0,0],[0,1,0],[0,0,1]) lbls_y2 = ["bar 2 " + lbl for lbl in lbl_type] fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax1.set_title("Single data") ax1.bar(x, y1, color='purple', linewidth=0, align='edge', width=0.8, label=lbl_y1) ax1.legend(loc=0) ax1.set_xticks(x) ax2 = fig.add_subplot(2,1,2, sharex=ax1) ax2.set_title("Multi data") for i, (y, color, lbl) in enumerate(zip(y2, colors_y2, lbls_y2)) : ax2.bar(x+(0.8*float(i)/len(y2)), y, color=color, label=lbl, linewidth=0, align='edge', width=(0.8/len(y2))) ax2.legend(loc=0) fig.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.95, wspace=0.1, hspace=0.15) plt.show() I hope this helps! Ben Root -- Learn how Oracle Real Application Clusters (RAC) One Node allows customers to consolidate database storage, standardize their database environment, and, should the need arise, upgrade to a full multi-node Oracle RAC database without downtime or disruption http://p.sf.net/sfu/oracle-sfdevnl___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Memory increasing while plotting in loop
Hi, I have a script that creates and saves figures in a loop. The memory is increasing at each figure and is not released back, rising a Memory error. I used the close() function on the figure object as well as gc.collect(), but no effect. I searched on the net and found a similar problem at http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size. The problem here was solved using the close() function but in my case, as refered before, it doens't work (see code below). I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on windows7. Thanks for help! Alain CODE: import pylab as pl import os, tempfile def plot_density(filename,i,t,psi_Na): pl.figure(figsize=(8,6)) pl.imshow(abs(psi_Na)**2,origin = 'lower') filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i) pl.savefig(filename) pl.clf() pl.close() if __name__ == "__main__": x = pl.linspace(-6e-6,6e-6,128,endpoint=False) y = pl.linspace(-6e-6,6e-6,128,endpoint=False) X,Y = pl.meshgrid(x,y) k = 100 omega = 200 times = pl.linspace(0,100e-3,100,endpoint=False) for i,t in enumerate(times): import pylab as pl psi_Na = pl.sin(k*X-omega*t) plot_density('wavefunction',i,t,psi_Na) print i -- EditLive Enterprise is the world's most technically advanced content authoring tool. Experience the power of Track Changes, Inline Image Editing and ensure content is compliant with Accessibility Checking. http://p.sf.net/sfu/ephox-dev2dev ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users