0

I have a csv file having following format Sample csv data

I need to plot graph for individual container CPUPerc, Memusage etc. against time stamp (time will be in x axis, CPU, memory value will be in Y axis) and once all the graphs generated, it should save all the graphs in a pdf file. I am able to generate all the graph with the following code, but when save it in pdf, it overwrites and saves only the last graph and not all. Can anyone help here.

Code is a below:

import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv('stats.csv')
mydict=["CPUPerc","MemUsage","NetIO","BlockIO","MemPerc"]
mp= df.groupby(['Container'])
for key,values in mp:
    m=mp.get_group(key)
    print key
    for j in mydict:
        y=m[j]
        z=[]
        for i in y:
            if ( j=="CPUPerc" or j=="MemPerc"):
                i=i.replace('%','')
                z.append(i)
            else:
                i,sep,tail = i.partition(' ')
                z.append(i)
        y=map(float,list(z))
        p=[]
        print y
        t=list()
        for i in xrange(len(y)):
            t.append(i)
        plt.plot(t,y)
        fig = plt.gcf()
        plt.xticks(rotation=45)
        ylab=('All {0} values for {1}------>'.format(j,key))
        plt.xlabel('Time Series ------->')
        plt.ylabel(ylab)
        #plt.grid(True)
        plt.grid(alpha=0.4)
        plt.tight_layout()
        plt.show()
        fig.savefig("All_Graph.pdf")
jit
  • 1
  • It saves only the last graph because you save all your graphs in the same file. You can or change the filename each time (you have a loop so it's easy), or have a look at this [example](http://matplotlib.org/examples/pylab_examples/multipage_pdf.html), it will enabled you to save your figures in a multipage pdf. – Nuageux May 04 '17 at 12:33
  • Look for answers of this question: https://stackoverflow.com/questions/17788685/python-saving-multiple-figures-into-one-pdf-file – Serenity May 04 '17 at 12:36

0 Answers0