Select to view content in your preferred language

Script won't stop running

391
5
07-24-2012 11:51 AM
StephenFricke
New Contributor III
I have created a function which plots data.  The problem is when I run the script it doesn't stop.  I know it is probably a really easy fix.  I would really appreciate some help!  Thanks!!

import numpy as np
import matplotlib.pyplot as plt
import datetime as DT

def plotGridmetOneDay(GcmVar,GcmYear,LastYear,output):
    with open(output+"/Climate.txt") as f:
        data = np.loadtxt(f, delimiter=',', skiprows=1,
            dtype={'names': ('date', 'count'),'formats': ('S10', 'i4')} )

    
        x = [DT.datetime.strptime(key,"%j/%Y") for (key, value) in data ]
        y = [value for (key, value) in data]

        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.grid()

        fig.autofmt_xdate()

        plt.plot(x,y)
        plt.xlabel('Date')
        plt.ylabel(str(GcmVar))
        plt.title(str(GcmVar)+" "+str(GcmYear)+"-"+str(LastYear))
        plt.savefig(output+"/ClimateMap.png",bbox_inches=0)
        plt.show()
        f.close()
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
You shouldn't need to manually close your file if you open it with the "with" statement. Try removing the
f.close()
and see what happens. Are you sure whatever reference you are using to this function isn't the problem?
0 Kudos
JasonScheirer
Regular Contributor II
Also get rid of the plt.show() and see if that helps.
0 Kudos
MikeHunter
Occasional Contributor
Ditto the other answers, plus you don't really need the with statement at all, since loadtxt accepts either a file object or filename as the first arg.  I'd feed it the filename and see if that doesn't help.  And there's some code somewhere that calls this function that we haven't seen.  Maybe your issue is that code, instead of this code.

Mike
0 Kudos
StephenFricke
New Contributor III
Thanks for the suggestions everyone.  It turns out the the plt.show() was causing the issue.  That is somewhat of a bummer because the alternative is to make the graph I am creating show up in the arcmap window, and it looks really bad with horrible resolution.  Does anyone know how to make .png or .jpg look sharper in the arcmap window?  Thanks!
0 Kudos
JasonScheirer
Regular Contributor II
Try using os.startfile(output+"/ClimateMap.png") instead.
0 Kudos