Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Matplotlib figure as TIFF

Tags:

Does anyone know how to save a Matplotlib figure as *.tiff? It seems that this format is not supported in Python, while the journals are quite often ask for that format.

I am adding some minimal code:

# -*- coding: utf-8 -*-  from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np  # fig setup fig = plt.figure(figsize=(5,5), dpi=300) ax = fig.gca(projection='3d') ax.set_xlim([-1,1]) ax.set_ylim([-1,1]) ax.set_zlim([-1,1]) ax.axes.xaxis.set_ticklabels([]) ax.axes.yaxis.set_ticklabels([]) ax.axes.zaxis.set_ticklabels([])  # draw a surface xx, yy = np.meshgrid(range(-1,2), range(-1,2)) zz = np.zeros(shape=(3,3)) ax.plot_surface(xx, yy, zz, color='#c8c8c8', alpha=0.3) ax.plot_surface(xx, zz, yy, color='#b6b6ff', alpha=0.2)  # draw a point ax.scatter([0],[0],[0], color='b', s=200) 

This works:

fig.savefig('3dPlot.pdf') 

But this does not:

fig.savefig('3dPlot.tif') 
like image 237
striatum Avatar asked Jun 21 '16 13:06

striatum


People also ask

How do I save a figure as a TIFF in Python?

savefig(fname, dpi=None, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None). Then I set dpi to 600 and . tiff as the output format. This works fine except that the file is very large ~32 mb.

How do I save a figure as a TIFF?

On the “File” menu, point to “Save as”, and then select “TIFF ” in the “File type” dialog box. 1. Select the figure in Excel or PowerPoint.

How do I save a figure in Matplotlib?

Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

How do I save a Matplotlib file as a JPEG?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.


1 Answers

As a workaround, there would be nothing to stop you using the Python PIL package to save your image in TIFF format:

# -*- coding: utf-8 -*-  from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np  from PIL import Image import io  # fig setup fig = plt.figure(figsize=(5,5), dpi=300) ax = fig.gca(projection='3d') ax.set_xlim([-1,1]) ax.set_ylim([-1,1]) ax.set_zlim([-1,1]) ax.axes.xaxis.set_ticklabels([]) ax.axes.yaxis.set_ticklabels([]) ax.axes.zaxis.set_ticklabels([])  # draw a surface xx, yy = np.meshgrid(range(-1,2), range(-1,2)) zz = np.zeros(shape=(3,3)) ax.plot_surface(xx, yy, zz, color='#c8c8c8', alpha=0.3) ax.plot_surface(xx, zz, yy, color='#b6b6ff', alpha=0.2)  # draw a point ax.scatter([0],[0],[0], color='b', s=200)  #fig.savefig('3dPlot.pdf')  # Save the image in memory in PNG format png1 = io.BytesIO() fig.savefig(png1, format="png")  # Load this image into PIL png2 = Image.open(png1)  # Save as TIFF png2.save("3dPlot.tiff") png1.close() 

If Python 2.x is being used, use cStringIO instead of BytesIO as follows:

import cStringIO  # Replace the BytesIO() call with png1 = cStringIO.StringIO() 
like image 65
Martin Evans Avatar answered Sep 22 '22 14:09

Martin Evans