<p>来源:<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html" rel="nofollow">numpy.genfromtxt</a></p>
<pre><code>numpy.genfromtxt Returns:ndarray
Data read from the text file. If usemask is True, this is a masked array.
</code></pre>
<p>@Ev。Kounis在评论部分指出:numpy.genfromtxt只返回一个数组,并将其分配给两个参数x和y</p>
<p>请更新您的代码如下:</p>
^{pr2}$
<p>例如</p>
<p>如果你的数据文件是这样的</p>
<pre><code>col1, col2, col3
1, 2, 3
10, 20, 30
100, 200, 300
</code></pre>
<p>然后,<code>numpy.genfromtxt</code>可以使用<code>names=True</code>选项将第一行解释为列标题。通过提供列标题,您可以非常方便地访问数据:</p>
<pre><code>data = np.genfromtxt('data.txt', delimiter=',', names=True)
print data['col1'] # Output: array([ 1., 10., 100.])
print data['col2'] # Output: array([ 2., 20., 200.])
print data['col3'] # Output: array([ 3., 30., 300.])
</code></pre>
<p>因为在这种情况下,数据是这样形成的</p>
<pre><code>row1, 1, 10, 100
row2, 2, 20, 200
row3, 3, 30, 300
</code></pre>
<p>使用以下代码片段可以实现类似的效果:</p>
<pre><code>labels = np.genfromtxt('data.txt', delimiter=',', usecols=0, dtype=str)
raw_data = np.genfromtxt('data.txt', delimiter=',')[:,1:]
</code></pre>
<p>第一行将第一列(标签)读入字符串数组。
第二行从文件中读取所有数据,但丢弃第一列。
这就是如何提取不同变量中的列。在</p>
<p>如果要绘制col1和col2,只需定义:</p>
<pre><code>x=data['col1']
y=data['col2']
</code></pre>