Python Forum
Saving multiple plots as pdf
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving multiple plots as pdf
#1
Good afternoon everyone,

I am trying to plot some figures and save them as pdf. For that, I have already written a script and the problem is that it is saving 5 pdf files but all five pdf files have the same image. Below is an excerpt from my script.
# plot data as graph, (0) none, (1) interactive, (2) pdf

plot = 2
if plot not in [1, 2]: sys.exit() # skip everything below
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [
r"\usepackage[utf8x]{inputenc}",
r"\usepackage[T1]{fontenc}",
r"\usepackage[detect-all]{siunitx}",
r'\usepackage{amsmath, amstext, amssymb}',
r'\usepackage{xfrac}',
r'\usepackage{lmodern, palatino, eulervm}']
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams.update({'font.size': 9})

# create figure suitable for A4 format
def mm2inch(*tupl):
 inch = 25.4
 if isinstance(tupl[0], tuple):
  return tuple(i/inch for i in tupl[0])
 else:
   return tuple(i/inch for i in tupl)

# line colours appropriate for colour-blind
Vermillion    = '#D55E00'
Blue          = '#0072B2'
BluishGreen   = '#009E73'
Orange        = '#E69F00'
SkyBlue       = '#56B4E9'
ReddishPurple = '#CC79A7'
Yellow        = '#F0E442'
Black         = '#000000'


save1 = 1
if save1 == 1:
 #-----------------------------------------------------------------------------------
 fig1 = plt.figure(num=1, figsize=mm2inch(160.0, 140.0), dpi=150)
 #-----------------------------------------------------------------------------------
 eFam  = np.max(np.abs(eF))
 uvam  = np.max(np.abs(uv15))
 ncl = 600 # number of countur levels, increase to make plot smoother but larger
 comap = 'RdGy'

 ax1 = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=1)
 ax1.set_xlabel(r"${r\Delta\theta}$ in $d$")
 ax1.set_ylabel(r"${r\Delta z}$ in $d$")
 cl1 = [-eFam*0.9,-eFam*0.8,-eFam*0.7, -eFam*0.6, -eFam*0.5,-eFam*0.4, -eFam*0.3,+eFam*0.3,+eFam*0.4,+eFam*0.5, +eFam*0.6, +eFam*0.7,+eFam*0.8, +eFam*0.9]
 cf1 = ax1.contour(th*r[k], z, np.array(eF).transpose(1,0), cl1, cmap='bwr', linestyles='dashed', linewidths=0.5) # , extend='both')
 cl2 = np.linspace(-uvam, +uvam, ncl) # set contour levels manually
 cf2 = ax1.contourf(th*r[k], z, np.array(uv15).transpose(1,0), cl2,  cmap=comap)
 ax1.set_aspect('equal') # makes axis ratio natural
 dvr = make_axes_locatable(ax1) # devider
 cbx = dvr.append_axes("top", size="5%", pad=0.1) # make colorbar axis
 cb1 = plt.colorbar(cf2, cax=cbx, ticks=[-uvam, 0, +uvam], orientation='horizontal') # set colorbar scale
 cbx.xaxis.set_ticks_position("top")
 #cb1.set_label(r"${\displaystyle\omega_z}$ ")

 if plot == 2:
  fig1.tight_layout()
  fnam1 = 'efluxQ2Q4Fourier'+'{:08d}'.format(iLast)+'.pdf'
  plt.savefig(fnam1)
  print('Written file', fnam1)
  #fig1.clf()

save2 = 1
if save2 == 1:
 #---------------------------------------------------------------
 fig2 = plt.figure(num=2, figsize=mm2inch(160.0, 140.0), dpi=150)
 #---------------------------------------------------------------
 ePam  = np.max(np.abs(eP))
 uvEam = np.max(np.abs(uvE15))
 ncl = 600 # number of countur levels, increase to make plot smoother but larger
 comap = 'RdGy'

 ax2 = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=1)
 ax2.set_xlabel(r"${r\Delta\theta}$ in $d$")
 ax2.set_ylabel(r"${r\Delta z}$ in $d$")
 cl3 = [+ePam*0.3, +ePam*0.4, +ePam*0.5, +ePam*0.6, +ePam*0.7, +ePam*0.8, +ePam*0.9]
 cf3 = ax2.contour(th*r[k], z, np.array(eP).transpose(1,0), cl3, colors='r', linestyles='dashed', linewidths=0.5) # , extend='both')
 cl4 = np.linspace(-uvEam, +uvEam, ncl) # set contour levels manually
 cf4 = ax2.contourf(th*r[k], z, np.array(uvE15).transpose(1,0), cl4,  cmap=comap)
 ax2.set_aspect('equal') # makes axis ratio natural
 dvr = make_axes_locatable(ax2) # devider
 cbx = dvr.append_axes("top", size="5%", pad=0.1) # make colorbar axis
 cb3 = plt.colorbar(cf4, cax=cbx, ticks=[-uvEam, 0, +uvEam], orientation='horizontal') # set colorbar scale
 cbx.xaxis.set_ticks_position("top")
 #cb1.set_label(r"${\displaystyle\omega_z}$ ") 

 if plot == 2:
  fig2.tight_layout()
  fnam2 = 'efluxQ2Q4Fourier'+'{:08d}'.format(iLast)+'.pdf'
  plt.savefig(fnam2)
  print('Written file', fnam2)
  #fig2.clf()

save3 = 1
if save3 == 1:
 #---------------------------------------------------------------
 fig3 = plt.figure(num=3, figsize=mm2inch(160.0, 140.0), dpi=150)
 #---------------------------------------------------------------
 eNam  = np.max(np.abs(eN))
 uvEam = np.max(np.abs(uvE15))
 ncl = 600 # number of countur levels, increase to make plot smoother but larger
 comap = 'RdGy' 

 ax3 = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=1)
 ax3.set_xlabel(r"${r\Delta\theta}$ in $d$")
 ax3.set_ylabel(r"${r\Delta z}$ in $d$")
 cl5 = [-eNam*0.9, -eNam*0.8, -eNam*0.7, -eNam*0.6, -eNam*0.5, -eNam*0.4, -eNam*0.3]
 cf5 = ax3.contour(th*r[k], z, np.array(eN).transpose(1,0), cl5, colors='blue', linestyles='dashed', linewidths=0.5) # , extend='both')
 cl6 = np.linspace(-uvEam, +uvEam, ncl) # set contour levels manually
 cf6 = ax3.contourf(th*r[k], z, np.array(uvE15).transpose(1,0), cl6,  cmap=comap)
 ax3.set_aspect('equal') # makes axis ratio natural
 dvr = make_axes_locatable(ax3) # devider
 cbx = dvr.append_axes("top", size="5%", pad=0.1) # make colorbar axis
 cb5 = plt.colorbar(cf6, cax=cbx, ticks=[-uvEam, 0, +uvEam], orientation='horizontal') # set colorbar scale
 cbx.xaxis.set_ticks_position("top")
 #cb1.set_label(r"${\displaystyle\omega_z}$ ") 

 if plot == 2:
  fig3.tight_layout()
  fnam3 = 'efluxQ2Q4Fourier'+'{:08d}'.format(iLast)+'.pdf'
  plt.savefig(fnam3)
  print('Written file', fnam3)
  #fig3.clf()

save4 = 1
if save4 == 1:
 #---------------------------------------------------------------
 fig4 = plt.figure(num=4, figsize=mm2inch(160.0, 140.0), dpi=150)
 #---------------------------------------------------------------
 ePam  = np.max(np.abs(eP))
 uvSam = np.max(np.abs(uvS15))
 ncl = 600 # number of countur levels, increase to make plot smoother but larger
 comap = 'RdGy'

 ax4 = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=1)
 ax4.set_xlabel(r"${r\Delta\theta}$ in $d$")
 ax4.set_ylabel(r"${r\Delta z}$ in $d$")
 cl7 = [+ePam*0.3, +ePam*0.4, +ePam*0.5, +ePam*0.6, +ePam*0.7, +ePam*0.8, +ePam*0.9]
 cf7 = ax4.contour(th*r[k], z, np.array(eP).transpose(1,0), cl7, colors='r', linestyles='dashed', linewidths=0.5) # , extend='both')
 cl8 = np.linspace(-uvSam, +uvSam, ncl) # set contour levels manually
 cf8 = ax4.contourf(th*r[k], z, np.array(uvS15).transpose(1,0), cl8,  cmap=comap)
 ax4.set_aspect('equal') # makes axis ratio natural
 dvr = make_axes_locatable(ax4) # devider
 cbx = dvr.append_axes("top", size="5%", pad=0.1) # make colorbar axis
 cb7 = plt.colorbar(cf8, cax=cbx, ticks=[-uvSam, 0, +uvSam], orientation='horizontal') # set colorbar scale
 cbx.xaxis.set_ticks_position("top")
 #cb1.set_label(r"${\displaystyle\omega_z}$ ")

 if plot == 2:
  fig4.tight_layout()
  fnam4 = 'efluxQ2Q4Fourier'+'{:08d}'.format(iLast)+'.pdf'
  plt.savefig(fnam4)
  print('Written file', fnam4)
  #fig4.clf()

save5 = 1
if save5 == 1:
 #---------------------------------------------------------------
 fig5 = plt.figure(num=5, figsize=mm2inch(160.0, 140.0), dpi=150)
 #---------------------------------------------------------------
 eNam  = np.max(np.abs(eN))
 uvSam = np.max(np.abs(uvS15))
 ncl = 600 # number of countur levels, increase to make plot smoother but larger
 comap = 'RdGy' 

 ax5 = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=1)
 ax5.set_xlabel(r"${r\Delta\theta}$ in $d$")
 ax5.set_ylabel(r"${r\Delta z}$ in $d$")
 cl9 = [-eNam*0.9, -eNam*0.8, -eNam*0.7, -eNam*0.6, -eNam*0.5, -eNam*0.4, -eNam*0.3]
 cf9 = ax5.contour(th*r[k], z, np.array(eN).transpose(1,0), cl9, colors='blue', linestyles='dashed', linewidths=0.5) # , extend='both')
 cl10 = np.linspace(-uvEam, +uvEam, ncl) # set contour levels manually
 cf10 = ax5.contourf(th*r[k], z, np.array(uvE15).transpose(1,0), cl10,  cmap=comap)
 ax5.set_aspect('equal') # makes axis ratio natural
 dvr = make_axes_locatable(ax5) # devider
 cbx = dvr.append_axes("top", size="5%", pad=0.1) # make colorbar axis
 cb5 = plt.colorbar(cf10, cax=cbx, ticks=[-uvSam, 0, +uvSam], orientation='horizontal') # set colorbar scale
 cbx.xaxis.set_ticks_position("top")
 #cb1.set_label(r"${\displaystyle\omega_z}$ ") 

 if plot == 2:
  fig5.tight_layout()
  fnam5 = 'efluxQ2Q4Fourier'+'{:08d}'.format(iLast)+'.pdf'
  plt.savefig(fnam5)
  print('Written file', fnam5)
  #fig5.clf()

#-----------------------------------------------------------------------------------
# plot mode interactive or pdf
if plot != 2:
 plt.tight_layout()
 plt.show()
Any kind of help will be greatly appreciated.

Have a nice day :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple Plots in Spyder PythonAndMe2023 0 868 Feb-03-2023, 07:00 PM
Last Post: PythonAndMe2023
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,657 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Multiple user defined plots with secondary axes using for loop maltp 1 1,439 Apr-30-2022, 10:19 AM
Last Post: maltp
  Subplot - Plotting 4 plots on the same row Menthix 1 1,417 Nov-07-2021, 09:03 PM
Last Post: deanhystad
  Sharing X Axis in Sub plots JoeDainton123 1 2,176 Aug-22-2020, 04:11 AM
Last Post: deanhystad
  Help With Sub Plots JoeDainton123 0 1,698 Aug-20-2020, 10:48 PM
Last Post: JoeDainton123
  Group scatter plots Mekala 0 1,643 Jul-23-2020, 02:18 PM
Last Post: Mekala
  How to plot multiple scatter plots in seaborn vikola 2 6,478 Jul-14-2019, 10:30 AM
Last Post: vikola
  Can't locate plots package BobLoblaw 12 10,928 Oct-04-2017, 11:56 PM
Last Post: BobLoblaw
  Adding text to plots shaynehansen 0 3,322 Jul-10-2017, 05:54 PM
Last Post: shaynehansen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020