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:
JavaScript
x
25
25
1
# -*- coding: utf-8 -*-
2
3
from mpl_toolkits.mplot3d import Axes3D
4
import matplotlib.pyplot as plt
5
import numpy as np
6
7
# fig setup
8
fig = plt.figure(figsize=(5,5), dpi=300)
9
ax = fig.gca(projection='3d')
10
ax.set_xlim([-1,1])
11
ax.set_ylim([-1,1])
12
ax.set_zlim([-1,1])
13
ax.axes.xaxis.set_ticklabels([])
14
ax.axes.yaxis.set_ticklabels([])
15
ax.axes.zaxis.set_ticklabels([])
16
17
# draw a surface
18
xx, yy = np.meshgrid(range(-1,2), range(-1,2))
19
zz = np.zeros(shape=(3,3))
20
ax.plot_surface(xx, yy, zz, color='#c8c8c8', alpha=0.3)
21
ax.plot_surface(xx, zz, yy, color='#b6b6ff', alpha=0.2)
22
23
# draw a point
24
ax.scatter([0],[0],[0], color='b', s=200)
25
This works:
JavaScript
1
2
1
fig.savefig('3dPlot.pdf')
2
But this does not:
JavaScript
1
2
1
fig.savefig('3dPlot.tif')
2
Advertisement
Answer
This is great! Thanks ot Martin Evans.
However, for those who would like to make it happen in Python3.x
, small fixes (since cStringIO
module is not available; and I would rather use BytesIO
)
JavaScript
1
34
34
1
# -*- coding: utf-8 -*-
2
3
from mpl_toolkits.mplot3d import Axes3D
4
import matplotlib.pyplot as plt
5
import numpy as np
6
7
from PIL import Image
8
from io import BytesIO
9
10
# fig setup
11
fig = plt.figure(figsize=(5,5), dpi=300)
12
ax = fig.gca(projection='3d')
13
ax.set_xlim([-1,1])
14
ax.set_ylim([-1,1])
15
ax.set_zlim([-1,1])
16
ax.axes.xaxis.set_ticklabels([])
17
ax.axes.yaxis.set_ticklabels([])
18
ax.axes.zaxis.set_ticklabels([])
19
20
# draw a point
21
ax.scatter([0],[0],[0], color='b', s=200)
22
23
# save figure
24
# (1) save the image in memory in PNG format
25
png1 = BytesIO()
26
fig.savefig(png1, format='png')
27
28
# (2) load this image into PIL
29
png2 = Image.open(png1)
30
31
# (3) save as TIFF
32
png2.save('3dPlot.tiff')
33
png1.close()
34