Daniel Wigmore
Daniel Wigmore

Reputation: 97

saving in pyplot

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()  

Upvotes: 1

Views: 808

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169264

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

Upvotes: 5

Related Questions