我试图在 python 的一个目录中绘制我的所有数据文件,然后保存生成的图。我不是在尝试编写高效的 python 代码(不幸的是):现在只是对绘制数据感兴趣。
我是 python 的新手,我的目标是在我的论文中组织数字。
所以使用下面的代码,这就是我想要做的:
- 我列出所有
.dat
文件glob
- 我用文件名填充一个数组
- 我遍历这个文件名数组和
plot_surface
每个数据文件
我试图绘制的数据在这里
向我吐出的错误是:
错误
File "/home/dnaneet/Research/test_collection/dftdata/profile2.py", line 59, in <module>
plot(x, y, z)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2286, in plot
ret = ax.plot(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3783, in plot
for line in self._get_lines(*args, **kwargs):
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 317, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 278, in _plot_args
raise ValueError, 'third arg must be a format string'
ValueError: third arg must be a format string
Python代码
import os import glob import sys import subprocess import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np from numpy import * from numpy.random import rand from pylab import pcolor, show, colorbar, xticks, yticks from pylab import * print 'Listing all profile/dat files' profilefilelist = glob.glob('*profile*.dat') raw_input('Press ENTER to continue...') print profilefilelist for i in profilefilelist: DATA = i def get_data(fname=DATA): '''Read 2d array of z coordinates from file. Convert to float values and wrap in a numpy array.''' with open(fname) as f: data = [map(float, line.split()) for line in f] return np.array(data) def plot(x, y, z): fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(x, y, z, rstride=5, cstride=5,cmap="binary",linewidth=0.1) ax.set_zlim3d(0.0,4.0) ax.set_xlabel('X',fontsize=16,fontweight="bold") ax.set_ylabel('Y',fontsize=16,fontweight="bold") ax.set_zlabel('h(X,T)',fontsize=16,fontweight="bold") savefig(os.getcwd()+DATA+'.pdf',figsize=(5,5),dpi=600) savefig(os.getcwd()+DATA+'.pdf',figsize=(5,5),dpi=600) if __name__ == '__main__': z = get_data() x = range(z.shape[0]) y = range(z.shape[1]) x, y = np.meshgrid(x, y) plot(x, y, z)
问题
这是因为我的缩进不佳还是这里发生了更险恶的事情?我应该怎么做才能纠正这些错误?
我不太了解python,并且一直试图远离mathematica(因为我将毕业并且再也买不起世界上的mathematicas或matlabs了)。
SE上提供的先前代码:
修改后的代码(基于答案)
import os import glob import sys import subprocess import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np from numpy import * from numpy.random import rand from pylab import pcolor, show, colorbar, xticks, yticks from pylab import * print 'Listing all profile/dat files' profilefilelist = glob.glob('*profile*.dat') raw_input('Press ENTER to continue...') print profilefilelist DATA=profilefilelist for i in DATA: def get_data(fname=i): '''Read 2d array of z coordinates from file. Convert to float values and wrap in a numpy array.''' with open(fname) as f: data = [map(float, line.split()) for line in f] return np.array(data) for i in DATA: def my_plot(x, y, z): fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(x, y, z, rstride=5, cstride=5,cmap="binary",linewidth=0.1) ax.set_zlim3d(0.0,4.0) ax.set_xlabel('X',fontsize=16,fontweight="bold") ax.set_ylabel('Y',fontsize=16,fontweight="bold") ax.set_zlabel('h(X,T)',fontsize=16,fontweight="bold") plt.show() # savefig(os.getcwd()+DATA+'.pdf',figsize=(5,5),dpi=600) # savefig(os.getcwd()+DATA+'.pdf',figsize=(5,5),dpi=600) if __name__ == '__main__': z = get_data() x = range(z.shape[0]) y = range(z.shape[1]) x, y = np.meshgrid(x, y) my_plot(x, y, z)