Cannot save to defined path
Please make sure these conditions are met
- [x] I have checked that this issue has not already been reported.
- [x] I have confirmed this bug exists on the latest version of scanpy.
- [ ] (optional) I have confirmed this bug exists on the main branch of scanpy.
What happened?
Hi,
Apologies if I've missed this addressed somewhere else, however I would like to save my figured to a defined directory. It doesn't seem I can do that without first changing the current working directory outside the line of code.
What is the best way to save my plot to a specific directory without having to change it each time using os.chdir?
I have seen this issue from 2 years ago but wondered if any changes have been made since.
Minimal code sample
output_dir_fig = "chosen/path/to/directory"
sc.pl.highest_expr_genes(adata, n_top=10, save= f"{output_dir_fig}/highest_expr_genes.png")
Error output
FileNotFoundError Traceback (most recent call last) Cell In[85], line 1 ----> 1 sc.pl.highest_expr_genes(adata, n_top=10, save= f"{output_dir_fig}/highest_expr_genes.png")
File /opt/anaconda3/envs/scanpy/lib/python3.11/site-packages/legacy_api_wrap/init.py:80, in legacy_api.
File /opt/anaconda3/envs/scanpy/lib/python3.11/site-packages/scanpy/plotting/_qc.py:105, in highest_expr_genes(adata, n_top, show, save, ax, gene_symbols, log, **kwds) 103 ax.set_xscale("log") 104 show = settings.autoshow if show is None else show --> 105 _utils.savefig_or_show("highest_expr_genes", show=show, save=save) 106 if show: 107 return None
File /opt/anaconda3/envs/scanpy/lib/python3.11/site-packages/scanpy/plotting/_utils.py:339, in savefig_or_show(writekey, show, dpi, ext, save) 337 show = settings.autoshow if show is None else show 338 if save: --> 339 savefig(writekey, dpi=dpi, ext=ext) 340 if show: ... -> 2456 fp = builtins.open(filename, "w+b") 2458 try: 2459 save_handler(self, fp, filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/sarapatti/Desktop/PhD_projects/Llyod_lab/Col1_scRNAseq/scripts_new/Python_notebooks/figures/highest_expr_genes/Users/sarapatti/Desktop/PhD_projects/Llyod_lab/Col1_scRNAseq/analysis/figures/highest_expr_genes.png' Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
Versions
scanpy==1.10.1
anndata==0.10.7
umap==0.5.5
numpy==1.26.4
scipy==1.13.0
pandas==2.2.2
scikit-learn==1.4.2
statsmodels==0.14.1
igraph==0.11.5
pynndescent==0.5.12