Is there a way in which I can save multiple plots in individual pdfs on python?

Viewed 18

Here is my code, it plots plenty of data which I want to save each plot into a separate pdf files, but with just plt.savefig I can only save one.

The first part is where it opens and reads the folders and files, and asign values to the arrays.

Carpeta = "C:/Users/Aguirre/data/_TESS_lightcurves_median_after_detrended" 

for root, subdirectorios, archivos in os.walk(Carpeta): 
for subdirectorio in subdirectorios: 
    print(os.path.join(root, subdirectorio))
for archivo in archivos:  
    x = []
    y = []
    yerror = []
    c = os.path.join(root,archivo)
    print(c)
    for columnas in open(c):  
        values = [i for i in columnas.split()] 
        if is_number(values[0]) == True:
            x.append(float(values[0]))
        else: None
        if is_number(values[3]) == True:
            yerror.append(float(values[3]))
        else: None
        if is_number(values[1]) == False:
            del x[-1]
            del yerror[-1]
        else: 
            y.append(float(values[1]))
        

Here is were the plotting part starts

    ax = plt.gca()
    ax.invert_yaxis()

    plt.title(archivo)
    plt.xlabel('Julian Date')
    plt.ylabel('mag clean')
    plt.ticklabel_format(axis = 'both' , style = 'plain')

    markers, caps, bars = plt.errorbar(x,y,yerr=yerror, fmt='o', ecolor='black', capsize = 2, capthick = 2, markersize=1, color='k')
    [bar.set_alpha(0.1) for bar in bars]
    [cap.set_alpha(0.1) for cap in caps]

    plt.savefig("archivo.pdf",format="pdf", bbox_inches="tight")
    plt.show()
0 Answers
Related