python matplotlib bars don't change when running script multiple times
<p>I've been writing a post-processing script for a model we have at work, which worked fine. Recently I re-structured the script for clarity and re-usability.</p>

<p>General background: we have pickle-stored arrays as output from a model, for each "main parameter" <code>(N:nitrogen, P:phosporus)</code>. There are a bunch of pickled arrays per <code>subparameter/year/month</code> in a folder structure. Eg: <code>N/RNO3_2010_03</code>, is the result for March 2010 of the subparameter <code>RNO3</code> of the main parameter N.</p>

<p>What I do in the script below is give some parameters to a <code>def()</code> to run over the folder with the arrays, gather the information and plot a chart (optional write a survey of the data to CSV or make some PNG saves for visual purposes).</p>

<p>When I run this script for one main parameter (only <code>N</code> set to True, or only <code>P</code> set to True, all goes well). When I run this for both parameters at once (<code>N</code> and <code>P</code> both <code>True</code>), the plot for the second parameters seems to be the same as the plot for the first parameter. I looked up what could cause it, but can't find a similar problem.</p>

<p>The last thing I did was deleting the <code>p0</code>, <code>p1..</code>. parameters since they seem to be the ones that don't update (or keep the last input stored when starting a new loop).</p>

<p>Thanks for your advice!</p>

<p>Example of a correct output for N (sorry I can only upload 1 image).</p>

<pre><code>import numpy as np
import csv
import pickle
import matplotlib.pyplot as plt
import matplotlib
# Fill in settings below ! --------------------------------------------------------------------
path = "Z:/Modellering/2_ArcNemo/OutputArcNEMO/Rob/Nete20002012_20160802145344/"
bekken = "Nete"
N = True #set this True if you want to proces data for N, else False
P = True #set this True if you want to proces data for P, else False
RES = True #set this True if you want to proces data for residue, else False
data =  False #set this True if you want to write processed data to a file (csv), else False
figure = True #set this True if you want to visualise processed data in a figure (png)
save_png= False #set this True if you want to save a png of every array of RES, else False
years = ["2010","2011","2012"] #copy-paste the wanted years from below into this list
#"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018","2019","2020","2021"
#Fill in the above settings ! ------------------------------------------------------------------
parametersN = [N,"N",["DrNO3","RNO3","RNorg"],500000]
parametersP = [P,"P",["DroP","RoP","RPorg","RPsor"],5000]
#parametersRES = ["NS30","NS60","NS90"]
months = ["01","02","03","04","05","06","07","08","09","10","11","12"]
save = "overzicht.csv"
def process(parameters):
    dates = []
    max_par = 0
    S = 0
    par0_dat = []
    par1_dat = []
    par2_dat = []
    par3_dat = []
    result = []
    if parameters[0]:
        saved = path + str(parameters[1]) + save
        parameters_list = parameters[2]
        j=0
        for par in parameters_list:
            for y in years:
                for m in months:
                    to_load = path+str(parameters[1])+"/"+par+"_"+y+"_"+m
                    array = pickle.load(open(str(to_load),"rb"))
                    sum_par = np.sum(pickle.load(open(str(to_load),"rb")))/1000
                    if save_png:
                        save_img = path+par+"_"+y+"_"+m+".png"
                        matplotlib.image.imsave(save_img, array)
                    if figure==True:
                        dates.append(str(m)+"/"+str(y))
                        if j==0:
                            par0_dat.append(sum_par)
                        if j==1:
                            par1_dat.append(sum_par)
                        if j==2:
                            par2_dat.append(sum_par)
                        if j==3:
                            par3_dat.append(sum_par)
                    if data==True:
                        result.append(str(par))
                        d = str(m) + "_" + str(y)
                        result.append(d)
                        result.append(str(sum_par))
                        with open(saved, 'ab') as f:
                            w = csv.writer(f, delimiter = ",")
                            w.writerow(result)
                            result = []
            j+=1
        if figure==True:
            S = len(dates)/len(parameters[2])
            if parameters[1]!="P":
                par3_dat = [0]*S
            for i in range(0, S):
                if (par0_dat[i] + par1_dat[i] + par2_dat[i] + par3_dat[i]) &gt; max_par:
                    max_par = par0_dat[i] + par1_dat[i] + par2_dat[i] + par3_dat[i]
            ind = np.arange(S)
            width = 0.6
            p1 = plt.bar(ind, par0_dat, width, color='r')
            p2 = plt.bar(ind, par1_dat, width, color='y',bottom=par0_dat)
            p3 = plt.bar(ind, par2_dat, width, color='b',bottom=[par0_dat[k]+par1_dat[k] for k in range(len(par0_dat))])
            p4 = plt.bar(ind, par3_dat, width, color='k',bottom=[par0_dat[l]+par1_dat[l]+par2_dat[l] for l in range(len(par0_dat))])
            if parameters[1]!="P":
                plt.legend((p1[0], p2[0], p3[0]), (parameters[2][0], parameters[2][1] , parameters[2][2]))
            else:
                plt.legend((p1[0], p2[0], p3[0], p4[0]), (parameters[2][0], parameters[2][1] , parameters[2][2], parameters[2][3]))
            title = parameters[1] + "t netto-emissies " + bekken + " " + str(years[0]) +" - " + str(years[len(years)-1])
            plt.title(title)
            plt.ylabel("kg " + parameters[1] + "/mnd")
            plt.xticks(ind + width/2., dates)
            plt.xticks(rotation=90, fontsize=7)
            plt.yticks(np.arange(0, max_par, parameters[3]))
            plt.tight_layout()
            plt.savefig(path+ str(parameters[1]) +".png")
            print str(p1)
            print p1
            del p1
            del p2
            del p3
            del p4
            parameters = []
process(parametersN)
process(parametersP)
print "Done"
</code></pre>

<p>I guess the only thing you need is a new figure... so try to place <code>plt.figure()</code> right after <code>if figure == True:</code> like this:</p>

<pre><code>        ...
        j+=1
    if figure==True:
        plt.figure()
        S = len(dates)/len(parameters[2])
        ...
</code></pre>

<p>and see if it works.<br>
Or do you want to have both P and N results in one figure? Then you need to work with subplots but you'll have to change a little more.</p>

python matplotlib