2016-06-21 2 views
4

matplotlibの図を* .tiffとして保存する方法を知っている人はいますか?この形式はPythonではサポートされていないようですが、ジャーナルはしばしばその形式を求めています。Python matplotlibをTIFFとして保存

私はいくつかの最小限のコードを追加してい:

# -*- 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') 
+0

cStringIOモジュールが利用できないので、私はむしろBytesIOを使用しますか)?私はティファが写真や多分芸術的なイメージのようなものにしか使われないと思った。 – syntonym

+0

[生データをtifとして保存する]の複製があります。(0120-18751) – Deadpool

+0

残念ながら、彼らはtiff-sを求めています。私はそれが奇妙であることを知っている。また、私はそれで動作しませんでした。 – striatum

答えて

2

これは素晴らしいです!ありがとうot Martin Evans。それはPython3.xで実現するためにたい人のためしかし 、小さな修正は、ジャーナルが実際に生成された画像のTIFFファイルが必要です

# -*- 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() 
5

回避策として、止めることは何もないでしょう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 cStringIO 


# 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 = cStringIO.StringIO() 
fig.savefig(png1, format="png") 

# Load this image into PIL 
png2 = Image.open(png1) 

# Save as TIFF 
png2.save("3dPlot.tiff") 
png1.close() 
関連する問題