简体   繁体   中英

Python error : Extra plot while plotting multiple data files using glob

I have the following code where I'm plotting two data files "data_1.txt" (quadratic function) and "data_2.txt" (cubic function) using the glob with wildcard * .

import numpy as np
import matplotlib.pyplot as plt
import glob

#fig,ax = plt.subplots()

path = "data_*.txt"
for datafile in glob.glob(path):

    #data = np.loadtxt(datafile)
    data = np.genfromtxt(datafile)
    #print(data)
    #ax.plot(data)
    plt.plot(data)
    #plt.plot(data[:,0], data[:,1])  # This line works

plt.show()

However, this is generating an extra plot (red colored line)!

在此输入图像描述

If I use plt.plot(data[:,0], data[:,1]) instead of plt.plot(data) , the red line doesn't appear any more.

Any explanation? Also is there any difference between genfromtxt and loadtxt in this context?

The red line you see is really a blue line and a red line on top of each other. Those are the first columns of your data files plotted against their index.

In order to produce a line plot with matplotlib's plot function, one should specify the x and y coordinate array. One option is indeed to use

data = np.genfromtxt(datafile)
plt.plot(data[:,0],data[:,1])

Another option would be to unpack the data. So for a two column datafile,

x,y = np.genfromtxt(datafile, unpack=True)
plt.plot(x,y)

Both options are equally available with np.loadtxt . In the respective documentations you will find that genfromtxt has a lot more options than loadtxt . Those extra options may be usefull for excluding header or footer data or replacing missing values. The return type of both commands is the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM