Pylab.savefig DPI issue

Hi all,

I hope this is not a duplicate as I couldn´t find anything similar here.

I´m automating plotting data out of a sample dataset (10 data sets of different sizes within a .txt file).

I run matplotlib 3.5.3 as I get a Matplotlib Deprecation Warning with the newer version.

When I use

pylab.imshow(mat, cmap = cmap)
pylab.axis(‘off’)
pylab.savefig(id + “.png”, bbox_inches=‘tight’, pad_inches=0)

it only saves one data set as an image

when I use

pylab.savefig(seq_record.id + “.png”, bbox_inches=‘tight’, pad_inches=0, dpi=2000)

it saves five.

I tried to set dpi to 5000, it saves all files, but makes the whole script ridiculously slow.

The error I get for setting DPI to 2000 is this one:

Traceback (most recent call last):
File “automation.py”, line 55, in
pylab.savefig(id + “.png”, bbox_inches=‘tight’, pad_inches=0, dpi=2000)
File “python3.10/site-packages/matplotlib/pyplot.py”, line 977, in savefig
res = fig.savefig(*args, **kwargs)
File “python3.10/site-packages/matplotlib/figure.py”, line 3058, in savefig
self.canvas.print_figure(fname, **kwargs)
File “python3.10/site-packages/matplotlib/backend_bases.py”, line 2319, in print_figure
result = print_method(
File “python3.10/site-packages/matplotlib/backend_bases.py”, line 1648, in wrapper
return func(*args, **kwargs)
File “python3.10/site-packages/matplotlib/_api/deprecation.py”, line 415, in wrapper
return func(*inner_args, **inner_kwargs)
File “python3.10/site-packages/matplotlib/backends/backend_agg.py”, line 541, in print_png
mpl.image.imsave(
File “python3.10/site-packages/matplotlib/image.py”, line 1675, in imsave
image.save(fname, **pil_kwargs)
File “python3.10/site-packages/PIL/Image.py”, line 2320, in save
save_handler(self, fp, filename)
File “python3.10/site-packages/PIL/PngImagePlugin.py”, line 1374, in _save
ImageFile._save(im, _idat(fp, chunk), [(“zip”, (0, 0) + im.size, 0, rawmode)])
File “python3.10/site-packages/PIL/ImageFile.py”, line 510, in _save
encoder.setimage(im.im, b)
SystemError: tile cannot extend outside image

(I shortened the File names to hide my name)

I would like to set the resolution automatically so that it adapts to the size of the input data/resulting graph size.

Thanks for any help!

It it very hard to help you based on the information here. Can you give us a complete (that we can copy-paste-run) example that shows these problems?

Why are you trying to save at such high dpi?

I think the functionality you want is in matplotlib.figure — Matplotlib 3.6.0 documentation