擅长:python、mysql、java
<p>这就是<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html#numpy.loadtxt" rel="nofollow">^{<cd1>}</a>的设计目的。尝试:</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt(file, skiprows = 2) # assuming you have time and step information on 2 separate lines
# and you do not want to read them
plt.plot(data[:,0], data[:,1])
plt.show()
</code></pre>
<p>编辑:
如果您有散布在文件中的时间和步骤信息,并且希望在每个步骤上绘制数据,则有可能将所有文件读取到内存中(假设它足够小),然后将其拆分为<code>time</code>字符串:</p>
<pre><code>l = open(fname, 'rb').read()
for chunk in l.split('time'):
data = np.array([s.split() for s in chunk.split('\n')[2:]][:-1], dtype = np.float)
plt.plot(data[:,0], data[:,1])
plt.show()
</code></pre>
<p>或者您可以将<code>#</code>注释符号添加到注释行并使用<code>np.loadxt</code>。</p>