擅长:python、mysql、java
<p>在查看了<code>pyplot.py</code>包的内容之后,我意识到<code>plotfile</code>函数与子批的接口不好:如果您想将文件的多个列打印到子批,它可以很容易地做到这一点。你知道吗</p>
<p>如果您想任意地将多个(可能不同的)文件打印到不同的子图,那么它就不能</p>
<p>我找到的解决方案是使用<code>numpy</code><code>genfromtxt</code>通过编写我们自己的<code>plot_file</code>函数来读取数据:</p>
<pre><code>import numpy as np
def plot_file(ax, fnme, cols=[], label=None):
data = np.genfromtxt(
fnme,
skip_header=0,
skip_footer=0,
names=[str(col) for col in cols],
)
ax.plot(*[data[str(col)] for col in cols], label=label)
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 3))
PLOT_INDEXES = range(0,2)
for i in PLOT_INDEXES:
ax = plt.subplot(1, len(PLOT_INDEXES), i+1)
plot_file(ax, 'test_{0}.txt'.format(i), cols=[0, 1], label=str(i))
plt.show()
</code></pre>