problems doing MULTIPLE plots -> old plots appear on new plots!?!

Help! I'm trying to make multiple plots in a web app and old plots seem to
stick around and show up on new plots!??!

Is there someway to "erase the canvas" or avoid this happening?

Sound familiar?

growth function below is the first plot that is ok.
management function below is the one that has growth plot in it.

Chris

···

=====================================================================

import matplotlib
matplotlib.use("Agg")
import pylab

BACKGROUND_COLOR = "#ffffff"

def growth(company):
        """
        Plots Growth plot.
        """

        # Generates figure_, plot_ and functions.

        figure_ = pylab.figure()
        plot_ = pylab.subplot(111, axisbg=BACKGROUND_COLOR)
        years_plus_5 = company.years + range(max(company.years) + 1,
                                             max(company.years) + 6)
        five_percent = [1.05 ** (y - min(company.years))
                               for y in years_plus_5]
        ten_percent = [1.10 ** (y - min(company.years))
                               for y in years_plus_5]
        fifteen_percent = [1.15 ** (y - min(company.years))
                               for y in years_plus_5]
        twenty_percent = [1.20 ** (y - min(company.years))
                               for y in years_plus_5]
        twenty_five_percent = [1.25 ** (y - min(company.years))
                               for y in years_plus_5]
        thirty_percent = [1.30 ** (y - min(company.years))
                               for y in years_plus_5]
        scaled_sales = [50.0 * e / company.sales[-1]
                                for e in company.sales]
        scaled_earnings = [30.0 * e / company.earnings[-1]
                                for e in company.earnings]
        scaled_pretax_profit = [10.0 * e / company.pretax_profit[-1]
                                for e in company.pretax_profit]
        scaled_low_price = [100.0 * e / company.high_price[-1]
                                for e in company.low_price]
        scaled_high_price = [100.0 * e / company.high_price[-1]
                                for e in company.high_price]
        functions = plot_.semilogy(company.years, scaled_sales,
                                   company.years, scaled_earnings,
                                   company.years, scaled_pretax_profit,
                                   years_plus_5, five_percent,
                                   years_plus_5, ten_percent,
                                   years_plus_5, fifteen_percent,
                                   years_plus_5, twenty_percent,
                                   years_plus_5, twenty_five_percent,
                                   years_plus_5, thirty_percent)

        # Adds price bars and sets their thickness.

        offset = 0.12
        for i, y in enumerate(company.years):
                plot_.semilogy((y, y),
                               (scaled_low_price[i], scaled_high_price[i]),
                               linewidth="2.2", color = "black", zorder = 1)
                plot_.semilogy((y - offset, y + offset),
                               (scaled_low_price[i], scaled_low_price[i]),
                               linewidth="2.2", color = "black", zorder = 1)
                plot_.semilogy((y - offset, y + offset),
                               (scaled_high_price[i], scaled_high_price[i]),
                               linewidth="2.2", color = "black", zorder = 1)

        # Adds sales, earnings and pretax_profit points.

        points = []
        points.append(pylab.scatter(company.years, scaled_sales, 200,
                                  c = BACKGROUND_COLOR))
        points.append(pylab.scatter(company.years, scaled_earnings, 200,
                                  c = BACKGROUND_COLOR))
        points.append(pylab.scatter(company.years, scaled_pretax_profit, 200,
                                  c = BACKGROUND_COLOR))

        # Configures points.

        pylab.setp(points[0], linewidth = "1.3", edgecolor = "g", zorder = 12)
        pylab.setp(points[1], linewidth = "1.3", edgecolor = "b", zorder = 11)
        pylab.setp(points[2], linewidth = "1.3", edgecolor = "r", zorder = 10)

        # Configures earnings, sales and pretax_profit.

        pylab.setp(functions[0], linewidth = "3.0", color = "g", zorder = 9)
        pylab.setp(functions[1], linewidth = "3.0", color = "b", zorder = 8)
        pylab.setp(functions[2], linewidth = "3.0", color = "r", zorder = 7)

        # Configures percentage growth.

        for f in functions[-6:]:
                pylab.setp(f, linewidth = "0.5", color = "k", zorder = 5)

        # Configures grid.

        plot_.grid(True)
        grid_ = plot_.get_xgridlines() + plot_.get_ygridlines()
        pylab.setp(grid_, linestyle = "-", color = "k", linewidth = "0.5",
                   zorder = 5)
        plot_.set_axisbelow(True)

        # Sets view range for both axes.

        pylab.axis([min(company.years), max(company.years) + 5, 1, 200])

        # Specifies tick values, color, size and boldness.

        x_tick_values = years_plus_5
        x_tick_labels = len(x_tick_values) * [""]
        for i in range(1, len(x_tick_values), 2):
                x_tick_labels[i] = str(x_tick_values[i])
        pylab.xticks(x_tick_values, x_tick_labels,
                     color = "k", fontsize = 15, fontweight = "bold")
        y_tick_values = range(1, 11, 1) + range(10, 110, 10) + [200]
        y_tick_labels = len(y_tick_values) * [""]
        for e in [1, 5, 10, 50, 100, 200]:
                y_tick_labels[y_tick_values.index(e)] = str(e)
        pylab.yticks(y_tick_values, y_tick_labels,
                     color = "k", fontsize = 15, fontweight = "bold")

        # Adds labels for percentage growth lines.

        pylab.text(max(years_plus_5), 2.0, " 5%", fontsize = 15)
        pylab.text(max(years_plus_5), 3.8, " 10%", fontsize = 15)
        pylab.text(max(years_plus_5), 7.0, " 15%", fontsize = 15)
        pylab.text(max(years_plus_5), 13.0, " 20%", fontsize = 15)
        pylab.text(max(years_plus_5), 23.0, " 25%", fontsize = 15)
        pylab.text(max(years_plus_5), 40.0, " 30%", fontsize = 15)

        # Adds labels for earnings, sales and pretax profit points.

        for c in zip(company.years, scaled_sales):
                pylab.text(c[0], c[1], "S",
                           horizontalalignment = 'center',
                           verticalalignment = 'center',
                           color = "g",
                           clip_on = True,
                           zorder = 12,
                           fontweight = "bold",
                           fontsize = 15)
        for c in zip(company.years, scaled_earnings):
                pylab.text(c[0], c[1], "E",
                           horizontalalignment = 'center',
                           verticalalignment = 'center',
                           color = "b",
                           clip_on = True,
                           zorder = 11,
                           fontweight = "bold",
                           fontsize = 15)
        for c in zip(company.years, scaled_pretax_profit):
                pylab.text(c[0], c[1], "P",
                           horizontalalignment = 'center',
                           verticalalignment = 'center',
                           color = "r",
                           clip_on = True,
                           zorder = 10,
                           fontweight = "bold",
                           fontsize = 15)

        # Sets background of figure to be transparent.

        figure_.figurePatch.set_alpha(0.0)

        # Creates a PNG file.

        pylab.savefig("gnustocks/static/images/growth_plot.png", dpi = (100))

def management(company):
        figure_ = pylab.figure()

        t = pylab.arange(0.0, 2.0, 0.01)
        import math
        s = pylab.sin(2*math.pi*t)
        pylab.plot(t, s, linewidth=1.0)

        pylab.xlabel('time (s)')
        pylab.ylabel('voltage (mV)')
        pylab.title('About as simple as it gets, folks')
        pylab.grid(True)
        pylab.savefig("gnustocks/static/images/management.png", dpi = (100))

Try putting

pylab.close()

after each pylab.savefig()

Eric

chris@...1388... wrote:

···

Help! I'm trying to make multiple plots in a web app and old plots seem to
stick around and show up on new plots!??!

Is there someway to "erase the canvas" or avoid this happening?

Sound familiar?

growth function below is the first plot that is ok.
management function below is the one that has growth plot in it.

Chris

=====================================================================

import matplotlib
matplotlib.use("Agg")
import pylab

BACKGROUND_COLOR = "#ffffff"

def growth(company):
        """
        Plots Growth plot.
        """

        # Generates figure_, plot_ and functions.

        figure_ = pylab.figure()
        plot_ = pylab.subplot(111, axisbg=BACKGROUND_COLOR)
        years_plus_5 = company.years + range(max(company.years) + 1,
                                             max(company.years) + 6)
        five_percent = [1.05 ** (y - min(company.years))
                               for y in years_plus_5]
        ten_percent = [1.10 ** (y - min(company.years))
                               for y in years_plus_5]
        fifteen_percent = [1.15 ** (y - min(company.years))
                               for y in years_plus_5]
        twenty_percent = [1.20 ** (y - min(company.years))
                               for y in years_plus_5]
        twenty_five_percent = [1.25 ** (y - min(company.years))
                               for y in years_plus_5]
        thirty_percent = [1.30 ** (y - min(company.years))
                               for y in years_plus_5]
        scaled_sales = [50.0 * e / company.sales[-1]
                                for e in company.sales]
        scaled_earnings = [30.0 * e / company.earnings[-1]
                                for e in company.earnings]
        scaled_pretax_profit = [10.0 * e / company.pretax_profit[-1]
                                for e in company.pretax_profit]
        scaled_low_price = [100.0 * e / company.high_price[-1]
                                for e in company.low_price]
        scaled_high_price = [100.0 * e / company.high_price[-1]
                                for e in company.high_price]
        functions = plot_.semilogy(company.years, scaled_sales,
                                   company.years, scaled_earnings,
                                   company.years, scaled_pretax_profit,
                                   years_plus_5, five_percent,
                                   years_plus_5, ten_percent,
                                   years_plus_5, fifteen_percent,
                                   years_plus_5, twenty_percent,
                                   years_plus_5, twenty_five_percent,
                                   years_plus_5, thirty_percent)

        # Adds price bars and sets their thickness.

        offset = 0.12
        for i, y in enumerate(company.years):
                plot_.semilogy((y, y),
                               (scaled_low_price[i], scaled_high_price[i]),
                               linewidth="2.2", color = "black", zorder = 1)
                plot_.semilogy((y - offset, y + offset),
                               (scaled_low_price[i], scaled_low_price[i]),
                               linewidth="2.2", color = "black", zorder = 1)
                plot_.semilogy((y - offset, y + offset),
                               (scaled_high_price[i], scaled_high_price[i]),
                               linewidth="2.2", color = "black", zorder = 1)

        # Adds sales, earnings and pretax_profit points.

        points =
        points.append(pylab.scatter(company.years, scaled_sales, 200,
                                  c = BACKGROUND_COLOR))
        points.append(pylab.scatter(company.years, scaled_earnings, 200,
                                  c = BACKGROUND_COLOR))
        points.append(pylab.scatter(company.years, scaled_pretax_profit, 200,
                                  c = BACKGROUND_COLOR))

        # Configures points.

        pylab.setp(points[0], linewidth = "1.3", edgecolor = "g", zorder = 12)
        pylab.setp(points[1], linewidth = "1.3", edgecolor = "b", zorder = 11)
        pylab.setp(points[2], linewidth = "1.3", edgecolor = "r", zorder = 10)

        # Configures earnings, sales and pretax_profit.

        pylab.setp(functions[0], linewidth = "3.0", color = "g", zorder = 9)
        pylab.setp(functions[1], linewidth = "3.0", color = "b", zorder = 8)
        pylab.setp(functions[2], linewidth = "3.0", color = "r", zorder = 7)

        # Configures percentage growth.

        for f in functions[-6:]:
                pylab.setp(f, linewidth = "0.5", color = "k", zorder = 5)

        # Configures grid.

        plot_.grid(True)
        grid_ = plot_.get_xgridlines() + plot_.get_ygridlines()
        pylab.setp(grid_, linestyle = "-", color = "k", linewidth = "0.5",
                   zorder = 5)
        plot_.set_axisbelow(True)

        # Sets view range for both axes.

        pylab.axis([min(company.years), max(company.years) + 5, 1, 200])

        # Specifies tick values, color, size and boldness.

        x_tick_values = years_plus_5
        x_tick_labels = len(x_tick_values) * [""]
        for i in range(1, len(x_tick_values), 2):
                x_tick_labels[i] = str(x_tick_values[i])
        pylab.xticks(x_tick_values, x_tick_labels,
                     color = "k", fontsize = 15, fontweight = "bold")
        y_tick_values = range(1, 11, 1) + range(10, 110, 10) + [200]
        y_tick_labels = len(y_tick_values) * [""]
        for e in [1, 5, 10, 50, 100, 200]:
                y_tick_labels[y_tick_values.index(e)] = str(e)
        pylab.yticks(y_tick_values, y_tick_labels,
                     color = "k", fontsize = 15, fontweight = "bold")

        # Adds labels for percentage growth lines.

        pylab.text(max(years_plus_5), 2.0, " 5%", fontsize = 15)
        pylab.text(max(years_plus_5), 3.8, " 10%", fontsize = 15)
        pylab.text(max(years_plus_5), 7.0, " 15%", fontsize = 15)
        pylab.text(max(years_plus_5), 13.0, " 20%", fontsize = 15)
        pylab.text(max(years_plus_5), 23.0, " 25%", fontsize = 15)
        pylab.text(max(years_plus_5), 40.0, " 30%", fontsize = 15)

        # Adds labels for earnings, sales and pretax profit points.

        for c in zip(company.years, scaled_sales):
                pylab.text(c[0], c[1], "S",
                           horizontalalignment = 'center',
                           verticalalignment = 'center',
                           color = "g",
                           clip_on = True,
                           zorder = 12,
                           fontweight = "bold",
                           fontsize = 15)
        for c in zip(company.years, scaled_earnings):
                pylab.text(c[0], c[1], "E",
                           horizontalalignment = 'center',
                           verticalalignment = 'center',
                           color = "b",
                           clip_on = True,
                           zorder = 11,
                           fontweight = "bold",
                           fontsize = 15)
        for c in zip(company.years, scaled_pretax_profit):
                pylab.text(c[0], c[1], "P",
                           horizontalalignment = 'center',
                           verticalalignment = 'center',
                           color = "r",
                           clip_on = True,
                           zorder = 10,
                           fontweight = "bold",
                           fontsize = 15)

        # Sets background of figure to be transparent.

        figure_.figurePatch.set_alpha(0.0)

        # Creates a PNG file.

        pylab.savefig("gnustocks/static/images/growth_plot.png", dpi = (100))

def management(company):
        figure_ = pylab.figure()

        t = pylab.arange(0.0, 2.0, 0.01)
        import math
        s = pylab.sin(2*math.pi*t)
        pylab.plot(t, s, linewidth=1.0)

        pylab.xlabel('time (s)')
        pylab.ylabel('voltage (mV)')
        pylab.title('About as simple as it gets, folks')
        pylab.grid(True)
        pylab.savefig("gnustocks/static/images/management.png", dpi = (100))

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks for the email. That didn't fix it for some reason. Any else I'm
missing? I'm wondering if there is some way to tell Maplotlib to create a
DIFFERENT figure rather than mixing them all together.

Chris

···

On Thu, Dec 21, 2006 at 10:16:25PM -1000, Eric Firing wrote:

Try putting

pylab.close()

after each pylab.savefig()

It looks like you are using pylab?
How about pylab.figure()?

Cheers,
Alan Isaac

···

On Fri, 22 Dec 2006, chris@...1388... apparently wrote:

I'm wondering if there is some way to tell Maplotlib to
create a DIFFERENT figure

Thanks for email. Yes 1st message in this thread has code to create each
plot. Each one does a 'figure_ = pylab.figure()' already.

chris

···

On Fri, Dec 22, 2006 at 12:21:44PM -0500, Alan G Isaac wrote:

On Fri, 22 Dec 2006, chris@...1388... apparently wrote:
> I'm wondering if there is some way to tell Maplotlib to
> create a DIFFERENT figure

It looks like you are using pylab?
How about pylab.figure()?

Sorry I missed the thread, but each call to pylab.figure()
will give you a new active figure. Everything you plot will
then be plotted to that figure. Previous plots will not be
erased until you explicitly remove them. You can also get
the axes for a particular figure and plot directly to the
axis.

Probably you are not creating new figures when you think you
are...

hth,
Alan Isaac

···

On Fri, 22 Dec 2006, chris@...1388... apparently wrote:

1st message in this thread has code to create each plot.
Each one does a 'figure_ = pylab.figure()' already.

Alan G Isaac wrote:

1st message in this thread has code to create each plot. Each one does a 'figure_ = pylab.figure()' already.

Sorry I missed the thread, but each call to pylab.figure() will give you a new active figure. Everything you plot will then be plotted to that figure. Previous plots will not be erased until you explicitly remove them. You can also get the axes for a particular figure and plot directly to the axis.

Probably you are not creating new figures when you think you are...

I'm puzzled. The call to pylab.close() after the savefig should completely wipe out the current figure, shouldn't it?

And even without the close (which certainly should be used anyway), each function starts with a call to pylab.figure() which should start a *new* figure.

I can't look into it now, though.

Eric

···

On Fri, 22 Dec 2006, chris@...1388... apparently wrote:

hth,
Alan Isaac

1st message in this thread has code to create each plot.
Each one does a 'figure_ = pylab.figure()' already.

Alan G Isaac wrote:

Sorry I missed the thread, but each call to pylab.figure()
will give you a new active figure. Everything you plot will
then be plotted to that figure. Previous plots will not be
erased until you explicitly remove them. You can also get
the axes for a particular figure and plot directly to the
axis.

Probably you are not creating new figures when you think you
are...

I'm puzzled. The call to pylab.close() after the savefig should
completely wipe out the current figure, shouldn't it?

Just because the figure window is closed does not
mean the figure is deleted. Chris apparently
maintains a reference to the figure?

And even without the close (which certainly should be used anyway), each
function starts with a call to pylab.figure() which should start a new
figure.

Right! This my comment above.

Cheers,
Alan Isaac

···

On Fri, 22 Dec 2006, chris@...1388... apparently wrote:

On Fri, 22 Dec 2006, Eric Firing apparently wrote: