[Python] FileNotFoundError when attempting to use plt.savefig to save an EPS file

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    The error has been stated here: Python : FileNotFoundError [WinError 2] The system cannot find the file specified , subprocess.py:1582

    I wanna to know how to set shell=True when running a command, instead of changing the __init__(..., shell=True, ...) in subprocess.py.

    For instance, I run the command python plot.py and get the error as follows:

    File "...\.conda\envs\pinns-env\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
    FileNotFoundError: [WinError 2] The system cannot find the file specified


    how can I fix it?

    I have searched for many solutions, all of which only tell me to set shell=True, but I don't know how.


    The error message:

    <ipython-input-5-f7c8e3bd400a> in <module>
    39 ax.set_ylabel('$y$')
    40 ax.set_title('Vorticity', fontsize = 10)
    ---> 41 savefig("./figures/vorticity")

    d:\Learning\Graduate\PINNs-code\Utilities\plotting.py in savefig(filename, crop)
    52 # plt.savefig('{}.pgf'.format(filename), bbox_inches='tight', pad_inches=0)
    53 plt.savefig('{}.pdf'.format(filename), bbox_inches='tight', pad_inches=0)
    ---> 54 plt.savefig('{}.eps'.format(filename), bbox_inches='tight', pad_inches=0)
    55 else:
    56 # plt.savefig('{}.pgf'.format(filename))

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\pyplot.py in savefig(*args, **kwargs)
    686 def savefig(*args, **kwargs):
    687 fig = gcf()
    --> 688 res = fig.savefig(*args, **kwargs)
    689 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
    690 return res

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\figure.py in savefig(self, fname, frameon, transparent, **kwargs)
    2095 self.set_frameon(frameon)
    2096
    -> 2097 self.canvas.print_figure(fname, **kwargs)
    2098
    2099 if frameon:

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
    2073 orientation=orientation,
    2074 bbox_inches_restore=_bbox_inches_restore,
    -> 2075 **kwargs)
    2076 finally:
    2077 if bbox_inches and restore_bbox:

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backends\backend_ps.py in print_eps(self, outfile, *args, **kwargs)
    919
    920 def print_eps(self, outfile, *args, **kwargs):
    --> 921 return self._print_ps(outfile, 'eps', *args, **kwargs)
    922
    923 def _print_ps(self, outfile, format, *args,

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backends\backend_ps.py in _print_ps(self, outfile, format, papertype, dpi, facecolor, edgecolor, orientation, *args, **kwargs)
    944 self._print_figure_tex(outfile, format, dpi, facecolor, edgecolor,
    945 orientation, isLandscape, papertype,
    --> 946 **kwargs)
    947 else:
    948 self._print_figure(outfile, format, dpi, facecolor, edgecolor,

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backends\backend_ps.py in _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor, orientation, isLandscape, papertype, metadata, dryrun, bbox_inches_restore, **kwargs)
    1334 or rcParams['text.usetex']):
    1335 gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
    -> 1336 rotated=psfrag_rotated)
    1337 elif rcParams['ps.usedistiller'] == 'xpdf':
    1338 xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backends\backend_ps.py in gs_distill(tmpfile, eps, ptype, bbox, rotated)
    1469
    1470 gs_exe = ps_backend_helper.gs_exe
    -> 1471 if ps_backend_helper.supports_ps2write: # gs version >= 9
    1472 device_name = "ps2write"
    1473 else:

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backends\backend_ps.py in supports_ps2write(self)
    92 True if the installed ghostscript supports ps2write device.
    93 """
    ---> 94 return self.gs_version[0] >= 9
    95
    96 ps_backend_helper = PsBackendHelper()

    c:\Users\zjuer\.conda\envs\pinns-env\lib\site-packages\matplotlib\backends\backend_ps.py in gs_version(self)
    76
    77 s = subprocess.Popen(
    ---> 78 [self.gs_exe, "--version"], stdout=subprocess.PIPE)
    79 pipe, stderr = s.communicate()
    80 ver = pipe.decode('ascii')

    c:\Users\zjuer\.conda\envs\pinns-env\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds)
    674 c2pread, c2pwrite,
    675 errread, errwrite,
    --> 676 restore_signals, start_new_session)
    677 except:
    678 # Cleanup if the child failed starting.

    c:\Users\zjuer\.conda\envs\pinns-env\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    955 env,
    956 cwd,
    --> 957 startupinfo)
    958 finally:
    959 # Child is launched. Close the parent's copy of those pipe

    FileNotFoundError: [WinError 2] 系统找不到指定的文件。


    The full code:


    data_vort = scipy.io.loadmat('../Data/cylinder_nektar_t0_vorticity.mat')

    x_vort = data_vort\['x'\]
    y_vort = data_vort\['y'\]
    w_vort = data_vort\['w'\]
    modes = np.asscalar(data_vort\['modes'\])
    nel = np.asscalar(data_vort\['nel'\])

    xx_vort = np.reshape(x_vort, (modes+1,modes+1,nel), order = 'F')
    yy_vort = np.reshape(y_vort, (modes+1,modes+1,nel), order = 'F')
    ww_vort = np.reshape(w_vort, (modes+1,modes+1,nel), order = 'F')

    box_lb = np.array(\[1.0, -2.0\])
    box_ub = np.array(\[8.0, 2.0\])

    fig, ax = newfig(1.0, 1.2)
    ax.axis('off')

    ####### Row 0: Vorticity ##################
    gs0 = gridspec.GridSpec(1, 2)
    gs0.update(top=1-0.06, bottom=1-2/4 + 0.12, left=0.0, right=1.0, wspace=0)
    ax = plt.subplot(gs0\[:, :\])

    for i in range(0, nel):
    h = ax.pcolormesh(xx_vort\[:,:,i\], yy_vort\[:,:,i\], ww_vort\[:,:,i\], cmap='seismic',shading='gouraud', vmin=-3, vmax=3)
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    fig.colorbar(h, cax=cax)

    ax.plot(\[box_lb\[0\],box_lb\[0\]\],\[box_lb\[1\],box_ub\[1\]\],'k',linewidth = 1)
    ax.plot(\[box_ub\[0\],box_ub\[0\]\],\[box_lb\[1\],box_ub\[1\]\],'k',linewidth = 1)
    ax.plot(\[box_lb\[0\],box_ub\[0\]\],\[box_lb\[1\],box_lb\[1\]\],'k',linewidth = 1)
    ax.plot(\[box_lb\[0\],box_ub\[0\]\],\[box_ub\[1\],box_ub\[1\]\],'k',linewidth = 1)

    ax.set_aspect('equal', 'box')
    ax.set_xlabel('$x$')
    ax.set_ylabel('$y$')
    ax.set_title('Vorticity', fontsize = 10)
    savefig("./figures/vorticity")



    where the savefig is defined as follows:

    def savefig(filename, crop = True):
    if crop == True:
    #plt.savefig('{}.pgf'.format(filename), bbox_inches='tight', pad_inches=0)
    plt.savefig('{}.pdf'.format(filename), bbox_inches='tight', pad_inches=0)
    plt.savefig('{}.eps'.format(filename), bbox_inches='tight', pad_inches=0)
    else:
    #plt.savefig('{}.pgf'.format(filename))
    plt.savefig('{}.pdf'.format(filename))
    plt.savefig('{}.eps'.format(filename))

    Continue reading...

Compartilhe esta Página