From: dcarroll <spa...@gm...> - 2010-08-26 03:48:03
|
I am trying make a plot that is in a loop and replots when new data is updated. #IN A LOOP fig = plt.figure(1,[10,10]) fig.clf prop = matplotlib.font_manager.FontProperties(size=8) xMin = dates[1] xMax = dates[-1] #temperature plot yMin = min(temperatureSQLData) yMax = max(temperatureSQLData) minTemperature = "Min = %.3f " % min(temperatureSQLData) + 'deg ' + 'C' minTemperatureIndex = temperatureSQLData.index(yMin) maxTemperature = "Max = %.3f " % max(temperatureSQLData) + 'deg ' + 'C' maxTemperatureIndex = temperatureSQLData.index(yMax) ax1 = fig.add_subplot(511) ax1.cla plt.hold(True) plt.plot(dates,temperatureSQLData,'r') plt.plot(dates[minTemperatureIndex],yMin,'ro',label=minTemperature) plt.plot(dates[maxTemperatureIndex],yMax,'bo',label=maxTemperature) plt.hold(False) plt.title("Water Conditions for the last 7 Days.") plt.xlim([xMin,xMax]) plt.ylim([yMin-1,yMax+1]) plt.ylabel('Temperature') ax1.legend(numpoints=1,prop=prop) dateFormat = mpl.dates.DateFormatter('%m/%d/%Y') ax1.xaxis.set_major_formatter(dateFormat) daysLocation = mpl.dates.DayLocator() ax1.xaxis.set_major_locator(daysLocation) hoursLocation = mpl.dates.HourLocator(interval=4) ax1.xaxis.set_minor_locator(hoursLocation) The problem is that the number of items in my legend grows with each iteration of the loop. Any help would be appreciated! -- View this message in context: http://old.nabble.com/My-Legend-keeps-growing%21-Help-tp29538626p29538626.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
From: Tinne De L. <tin...@me...> - 2010-08-26 13:23:44
|
Hi , On Thu, Aug 26, 2010 at 05:47, dcarroll <spa...@gm...> wrote: > > I am trying make a plot that is in a loop and replots when new data is > updated. > > #IN A LOOP > > fig = plt.figure(1,[10,10]) > fig.clf > > prop = matplotlib.font_manager.FontProperties(size=8) > > xMin = dates[1] > xMax = dates[-1] > > #temperature plot > yMin = min(temperatureSQLData) > yMax = max(temperatureSQLData) > > minTemperature = "Min = %.3f " % min(temperatureSQLData) + 'deg ' + 'C' > minTemperatureIndex = temperatureSQLData.index(yMin) > > maxTemperature = "Max = %.3f " % max(temperatureSQLData) + 'deg ' + 'C' > maxTemperatureIndex = temperatureSQLData.index(yMax) > > ax1 = fig.add_subplot(511) > ax1.cla > > plt.hold(True) > plt.plot(dates,temperatureSQLData,'r') > plt.plot(dates[minTemperatureIndex],yMin,'ro',label=minTemperature) > plt.plot(dates[maxTemperatureIndex],yMax,'bo',label=maxTemperature) > plt.hold(False) > > plt.title("Water Conditions for the last 7 Days.") > > plt.xlim([xMin,xMax]) > plt.ylim([yMin-1,yMax+1]) > plt.ylabel('Temperature') > ax1.legend(numpoints=1,prop=prop) > > dateFormat = mpl.dates.DateFormatter('%m/%d/%Y') > ax1.xaxis.set_major_formatter(dateFormat) > daysLocation = mpl.dates.DayLocator() > ax1.xaxis.set_major_locator(daysLocation) > hoursLocation = mpl.dates.HourLocator(interval=4) > ax1.xaxis.set_minor_locator(hoursLocation) > > The problem is that the number of items in my legend grows with each > iteration of the loop. Any help would be appreciated! You can select yourself what you want to include in the legend by creating the legend as follows: ************************************** legendEntries=[] # list of plots that are going to be in the legend legendText=[] # list of text messages for the legend thisPlot = plot(x,y,'b*') # a plot command legendEntries.append(thisPlot) # only do this for the plots you want to add to the legend legendText.append("legend text") # only this for the plots you want to add to the legend lgd = legend(legendEntries,legendText,numpoints=1,prop=props,loc='upper right') # example of how to draw the legend ************************************** so you could clean the legendEntries and legendText at the beginning of every loop by calling legendEntries=[] legendText=[] Or if the legend is every time the same you can only create it in the first iteration of the loop and then do not change the legendEntries or legendtext any more. Tinne |
From: Ryan M. <rm...@gm...> - 2010-08-26 13:36:01
|
On Thu, Aug 26, 2010 at 8:23 AM, Tinne De Laet <tin...@me...> wrote: > On Thu, Aug 26, 2010 at 05:47, dcarroll <spa...@gm...> wrote: >> >> I am trying make a plot that is in a loop and replots when new data is >> updated. >> >> #IN A LOOP >> >> fig = plt.figure(1,[10,10]) >> fig.clf >> >> prop = matplotlib.font_manager.FontProperties(size=8) >> >> xMin = dates[1] >> xMax = dates[-1] >> >> #temperature plot >> yMin = min(temperatureSQLData) >> yMax = max(temperatureSQLData) >> >> minTemperature = "Min = %.3f " % min(temperatureSQLData) + 'deg ' + 'C' >> minTemperatureIndex = temperatureSQLData.index(yMin) >> >> maxTemperature = "Max = %.3f " % max(temperatureSQLData) + 'deg ' + 'C' >> maxTemperatureIndex = temperatureSQLData.index(yMax) >> >> ax1 = fig.add_subplot(511) >> ax1.cla >> >> plt.hold(True) >> plt.plot(dates,temperatureSQLData,'r') >> plt.plot(dates[minTemperatureIndex],yMin,'ro',label=minTemperature) >> plt.plot(dates[maxTemperatureIndex],yMax,'bo',label=maxTemperature) >> plt.hold(False) >> >> plt.title("Water Conditions for the last 7 Days.") >> >> plt.xlim([xMin,xMax]) >> plt.ylim([yMin-1,yMax+1]) >> plt.ylabel('Temperature') >> ax1.legend(numpoints=1,prop=prop) >> >> dateFormat = mpl.dates.DateFormatter('%m/%d/%Y') >> ax1.xaxis.set_major_formatter(dateFormat) >> daysLocation = mpl.dates.DayLocator() >> ax1.xaxis.set_major_locator(daysLocation) >> hoursLocation = mpl.dates.HourLocator(interval=4) >> ax1.xaxis.set_minor_locator(hoursLocation) >> >> The problem is that the number of items in my legend grows with each >> iteration of the loop. Any help would be appreciated! > > > > You can select yourself what you want to include in the legend by > creating the legend as follows: You can also leave out the label, or in a loop like this, conditionally assign a label of '_nolegend_', which suppresses adding it to the legend. This example shows how the different options work: import numpy as np import matplotlib.pyplot as plt x1,y1 = np.random.rand(2,10) x2,y2 = np.random.rand(2,10) x3,y3 = np.random.rand(2,10) x4,y4 = np.random.rand(2,10) plt.plot(x1, y1, label='first') plt.plot(x2, y2) # No label, so not included plt.plot(x3, y3, label='_nolegend_') # Actively suppress plt.plot(x4, y4, label='fourth') plt.legend() # Only shows lines 'first' and 'fourth' Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma |
From: John H. <jd...@gm...> - 2010-08-26 14:34:09
|
On Wed, Aug 25, 2010 at 10:47 PM, dcarroll <spa...@gm...> wrote: > > I am trying make a plot that is in a loop and replots when new data is > updated. > > #IN A LOOP > > fig = plt.figure(1,[10,10]) > fig.clf It looks like the problem is that you are not clearing your figure. clf is a function, so you need fig.clf() JDH |