将 Matplotlib 图另存为 TIFF
Save Matplotlib figure as TIFF
有谁知道如何将 Matplotlib 图形保存为 *.tiff? Python似乎不支持这种格式,而期刊经常要求这种格式。
我正在添加一些最少的代码:
# -*- 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)
这个有效:
fig.savefig('3dPlot.pdf')
但这不是:
fig.savefig('3dPlot.tif')
作为解决方法,没有什么可以阻止您使用 Python PIL 包以 TIFF 格式保存图像:
# -*- 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()
如果使用 Python 2.x,请使用 cStringIO
而不是 BytesIO
,如下所示:
import cStringIO
# Replace the BytesIO() call with
png1 = cStringIO.StringIO()
太棒了!谢谢你 Martin Evans。
然而,对于那些想要在 Python3.x
中实现它的人来说,小修复(因为 cStringIO
模块不可用;我宁愿使用 BytesIO
)
# -*- coding: utf-8 -*-
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from io import BytesIO
# 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 point
ax.scatter([0],[0],[0], color='b', s=200)
# save figure
# (1) save the image in memory in PNG format
png1 = BytesIO()
fig.savefig(png1, format='png')
# (2) load this image into PIL
png2 = Image.open(png1)
# (3) save as TIFF
png2.save('3dPlot.tiff')
png1.close()
Matplotlib does support tif since version 1.1 but the support is optional and not obvious. As long as you have pillow 已安装,您可以保存为 tif,就像可以保存为任何其他格式一样。因此,您的示例将只是:
# -*- coding: utf-8 -*-
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import PIL # not necessary but mustn't fail
# 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.tif')
您只需安装 Pillow 即可。
然后你可以使用这个:
fig.savefig('3dPlot.tiff')
有谁知道如何将 Matplotlib 图形保存为 *.tiff? Python似乎不支持这种格式,而期刊经常要求这种格式。
我正在添加一些最少的代码:
# -*- 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)
这个有效:
fig.savefig('3dPlot.pdf')
但这不是:
fig.savefig('3dPlot.tif')
作为解决方法,没有什么可以阻止您使用 Python PIL 包以 TIFF 格式保存图像:
# -*- 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()
如果使用 Python 2.x,请使用 cStringIO
而不是 BytesIO
,如下所示:
import cStringIO
# Replace the BytesIO() call with
png1 = cStringIO.StringIO()
太棒了!谢谢你 Martin Evans。
然而,对于那些想要在 Python3.x
中实现它的人来说,小修复(因为 cStringIO
模块不可用;我宁愿使用 BytesIO
)
# -*- coding: utf-8 -*-
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from io import BytesIO
# 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 point
ax.scatter([0],[0],[0], color='b', s=200)
# save figure
# (1) save the image in memory in PNG format
png1 = BytesIO()
fig.savefig(png1, format='png')
# (2) load this image into PIL
png2 = Image.open(png1)
# (3) save as TIFF
png2.save('3dPlot.tiff')
png1.close()
Matplotlib does support tif since version 1.1 but the support is optional and not obvious. As long as you have pillow 已安装,您可以保存为 tif,就像可以保存为任何其他格式一样。因此,您的示例将只是:
# -*- coding: utf-8 -*-
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import PIL # not necessary but mustn't fail
# 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.tif')
您只需安装 Pillow 即可。 然后你可以使用这个:
fig.savefig('3dPlot.tiff')