本文介绍了将 Matplotlib 图另存为 TIFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道如何将Matplotlib图形保存为* .tiff吗?似乎Python不支持这种格式,而期刊却经常要求使用这种格式.
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)
这有效:
fig.savefig('3dPlot.pdf')
但这不是:
fig.savefig('3dPlot.tif')
推荐答案
这太棒了!感谢 Martin Evans.但是,对于那些想在 Python3.x
中实现它的人,进行了一些小修正(因为 cStringIO
模块不可用;我宁愿使用 BytesIO
)
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
)
# -*- 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 图另存为 TIFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!