0

I am trying to save both plots using same function with:

``` plot_xy(x,y,x1,y1)``` 

``` plot_xy(x=x,y=y,x1=x2,y1=y2)``` 

However, the output from the second is overwriting the first output, how can i change this to give me both figure with save.fig ?

Example code is listed below:

import numpy as np 
import matplotlib.pyplot as plt



def plot_xy(x,y,x1,y1,*args, **kwargs):
    A = np.amax(x1)
    fig, ax = plt.subplots()
    ax.plot(x, y, 'r-', label='a' )
    ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
    ax.set(xlabel='cm', ylabel='(d/t)',
           title='ABC')
    
    ax.grid()
    ax.set_ylim(ymin=0)
    ax.set_xlim(xmin=0)
    ax.set_ylim(ymax=1)
    ax.set_xlim(xmax=A) 

    plt.legend(loc = 'best')

    plt.show()
    fig.savefig(r'C:\...location....\test.png')
    
    return fig 



    
if __name__ == "__main__": 
    """passing a rough estimated data"""
    print (0)    
    
    x = np.linspace(0,500, num=20 )
    y = np.linspace(0,0.85, num=20 )  
    
    x1 = np.linspace(0,1000, num=20 )
    y1 = np.linspace(0,1000, num=20 )
    
    x2 = np.linspace(0,2000, num=20 )
    y2 = np.linspace(0,1, num=20 )
    
    plot_xy(x,y,x1,y1)
    
    plot_xy(x=x,y=y,x1=x2,y1=y2)


Thank you, all your effort is appreciated.

bmaster69
  • 121
  • 9

2 Answers2

1

This if because the file name is same. So the previous file is getting overwritten. You can handle this by making a new file name. You can use current seconds to make sure it is different. Or you can use any other type so that the generated file names are different every time.

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



def plot_xy(x,y,x1,y1,*args, **kwargs):
    A = np.amax(x1)
    fig, ax = plt.subplots()
    ax.plot(x, y, 'r-', label='a' )
    ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
    ax.set(xlabel='cm', ylabel='(d/t)',
           title='ABC')
    
    ax.grid()
    ax.set_ylim(ymin=0)
    ax.set_xlim(xmin=0)
    ax.set_ylim(ymax=1)
    ax.set_xlim(xmax=A) 

    plt.legend(loc = 'best')

    plt.show()
    fig.savefig(r'C:\...location....\test_' + str(datetime.datetime.now().strftime('%f'))+'.png')
    
    return fig 



    
if __name__ == "__main__": 
    """passing a rough estimated data"""
    print (0)    
    
    x = np.linspace(0,500, num=20 )
    y = np.linspace(0,0.85, num=20 )  
    
    x1 = np.linspace(0,1000, num=20 )
    y1 = np.linspace(0,1000, num=20 )
    
    x2 = np.linspace(0,2000, num=20 )
    y2 = np.linspace(0,1, num=20 )
    
    plot_xy(x,y,x1,y1)
    
    plot_xy(x=x,y=y,x1=x2,y1=y2)
Epsi95
  • 8,832
  • 1
  • 16
  • 34
0

This is because you are using the same figure save location for every call of the function. You can use another parameter for where to save the figure. Like this:

import numpy as np 
import matplotlib.pyplot as plt



def plot_xy(filename,x,y,x1,y1,*args, **kwargs):
    A = np.amax(x1)
    fig, ax = plt.subplots()
    ax.plot(x, y, 'r-', label='a' )
    ax.plot(x1, y1, linestyle='none', marker='^', label = 'b')
    ax.set(xlabel='cm', ylabel='(d/t)',
           title='ABC')
    
    ax.grid()
    ax.set_ylim(ymin=0)
    ax.set_xlim(xmin=0)
    ax.set_ylim(ymax=1)
    ax.set_xlim(xmax=A) 

    plt.legend(loc = 'best')

    plt.show()
    
    fig.savefig(f"{filename}.png")



    
if __name__ == "__main__": 
    """passing a rough estimated data"""
    print (0)    
    
    x = np.linspace(0,500, num=20 )
    y = np.linspace(0,0.85, num=20 )  
    
    x1 = np.linspace(0,1000, num=20 )
    y1 = np.linspace(0,1000, num=20 )
    
    x2 = np.linspace(0,2000, num=20 )
    y2 = np.linspace(0,1, num=20 )
    
    plot_xy("image1",x,y,x1,y1)
    
    plot_xy("image2",x=x,y=y,x1=x2,y1=y2)

DapperDuck
  • 2,728
  • 1
  • 9
  • 21