Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将Matplotlib图形另存为TIFF_Python_Matplotlib_Tiff - Fatal编程技术网

Python 将Matplotlib图形另存为TIFF

Python 将Matplotlib图形另存为TIFF,python,matplotlib,tiff,Python,Matplotlib,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

有人知道如何将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()
这太棒了!谢谢你。 但是,对于那些希望在
Python3.x
中实现这一点的人来说,有一些小的修正(因为
cStringIO
模块不可用;我宁愿使用
BytesIO

但支持是可选的,并不明显。只要已安装,就可以像保存到任何其他格式一样保存到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')

您只需安装枕头即可。 然后您可以使用以下命令:

fig.savefig('3dPlot.tiff')

很遗憾,他们确实要求tiff-s。我知道这很奇怪。另外,我没有使用它。如果你不需要精确的颜色复制,你可以简单地保存为png并将png文件转换为tiff。我以前做过,我不认为这是重复的。并不是每个情节都是原始图像。例如,如果我们从一个简单的
绘图(x,y)
开始,我们根本不清楚如何使用PIL/Pillow将其保存为
tif
。如果需要将条形图保存为tiff图像,该怎么办?我应该为“fig”分配什么?
fig.savefig('3dPlot.tiff')