1

i am trying to make a mainloop which automatically constructs and saves graphs, however i find that this is impossible and that i need to select each graph individually, open/show it and then save it manually. Does anyone know how to automate the process. the program reads a csv file and generates the graph from the rows specified in the argument.heres the code below and i cannot see where i am going wrong.

import matplotlib.pyplot as plt
import sys, csv, math

def main():
     readAndChart('8to2Ant10Average', 0,1,2, '8to2Ant10: cumulative ethnic and political attacks')
     readAndChart('8to2Ant10Average',0,8,9, '8to2Ant10: ethnic and political attacks per turn')
     readAndChart('8to2Ant10Average',0,11,12, '8to2Ant10: ethnic and political attacks as a percentage of total attacks')

def roundUp(x,y):
    roun = int(math.ceil(x/y))    
    k = int(roun*y)
    return k

def readAndChart(readTitle, r1,r2,r3, graphTitle):
    time = []
    data1 = []
    data2 = []
    data3 = []
    data4 = []
    read = csv.reader(open(readTitle + '.csv', 'rb'))
    legend1 = ''
    legend2 = ''

    for row in read:
        if row[0] == 'turn':
            legend1 = row[r2] 
            legend2 = row[r3]

        if row[0] != 'turn':
            a = row[r1]            
            b = row[r2]
            c = row[r3]

            a = float(a); b = float(b); c = float(c)
            time.append(a)
            data1.append(b)
            data2.append(c)

    axese = []  
    mt = max(time)
    mph = max(data1)
    mpd = max(data2)

    axese.append(mph)
    axese.append(mpd)

    ax = max(axese)
    print ax
    if ax < 10 and ax > 1:
        k = roundUp(ax, 10.0)

        plt.axis([0,mt, 0, k])
    if ax < 100 and ax > 10:
        k = roundUp(ax, 10.0)
        plt.axis([0,mt, 0, k])
    if ax < 1000 and ax > 100:
        k = roundUp(ax, 100.0)
        plt.axis([0,mt, 0, k])
    if ax < 10000 and ax > 1000:
        k = roundUp(ax, 500.0)        
        plt.axis([0,mt, 0, k])

    plt.xlabel('generation')
    plt.ylabel('fequency')
    plt.title(graphTitle)
    plt.plot(time, data1, 'r' )
    plt.plot(time, data2, 'b')

    plt.legend((legend1, legend2),'upper center', shadow=True, fancybox=True)
    plt.savefig(graphTitle + '.png')

if __name__=='__main__': main()  
5
  • Your code looks fine with one small exception: if you are on Windows I don't think you can put a : in a filename. As a simple fix you could do: plt.savefig(graphTitle.replace(':','') + '.png') Commented May 25, 2012 at 18:18
  • thankyou that did the trick, however now all the graphs have additional lines that are not called in their particular part of the main() function. sorry to pester you but you got any ideas? Commented May 25, 2012 at 19:26
  • Ah, didn't notice that before... You might try clearing the figure and/or axes after savefig() call, e.g.: plt.clf(); plt.cla(). After that line you could also do: plt.close() to close the current figure. Commented May 25, 2012 at 19:33
  • thanks for the help bernie, you have made my life immeasurably easier, ta again Commented May 25, 2012 at 19:48
  • Cheers, mate. Per this site's protocol I have posted my comments as an answer. Feel free to accept it (click checkmark) to help other users of the site more-easily see that your questions have been answered. Happy coding. Commented May 25, 2012 at 19:54

1 Answer 1

5

Problem 1:
It appears you are on Windows.
Windows does not allow you to put a colon (:) in a file-name.
Reference to support above statement: http://support.microsoft.com/kb/289627

Even if you are in *nix, I would recommend against using a colon in a file-name because some software may not be able to handle it properly.

A quick fix:

plt.savefig(graphTitle.replace(':','') + '.png')

Problem 2:
As mentioned in the comments, you also need to clear the figure after saving it.
Example:

plt.savefig(graphTitle.replace(':','') + '.png')
plt.clf()
plt.cla()

Or maybe just:

plt.savefig(graphTitle.replace(':','') + '.png')
plt.close() # call w/ no args to close current figure
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.