在循环中从多个文本文件创建子图

时间:2016-03-30 12:08:21

标签: matplotlib python-3.5 subplot

所以我一直在尝试将文本文件加载到多个子图上,但这些图似乎总是作为一个文本文件出现。任何人都可以指出我正确的#direction如何解决这个问题?

import numpy as np 
import matplotlib.pyplot as plt

RiverData1 = np.loadtxt('Gray1961.txt', skiprows = 2)

RiverData2 = np.loadtxt('Hack1957.txt', skiprows = 2)

RiverData3 = np.loadtxt('Rignon1996.txt', skiprows = 2)

RiverData4 = np.loadtxt('Robert1990.txt', skiprows = 2)

RiverData5 = np.loadtxt('Langbein1947_p145.txt', skiprows = 2)

RiverData6 = np.loadtxt('Langbein1947_p146.txt', skiprows = 2)

RiverData7 = np.loadtxt('Langbein1947_p149.txt', skiprows = 2)

RiverData8 = np.loadtxt('Langbein1947_p152.txt', skiprows = 2)

plotnums = 1    

for plotnums in range (1,9):
    plt.subplot(2,4,plotnums)
    plt.plot((RiverData1[:,0]), (RiverData1[:,1]),'ko') 
    plt.plot((RiverData2[:,0]), (RiverData2[:,1]),'ko')
    plt.plot((RiverData3[:,0]), (RiverData3[:,1]),'ko')
    plt.plot((RiverData4[:,0]), (RiverData4[:,1]),'ko')
    plt.plot((RiverData5[:,0]), (RiverData5[:,1]),'ko')
    plt.plot((RiverData6[:,0]), (RiverData6[:,1]),'ko')
    plt.plot((RiverData7[:,0]), (RiverData7[:,1]),'ko')
    plt.plot((RiverData8[:,0]), (RiverData8[:,1]),'ko')
    plt.xlabel('River Length (km)')
    plt.ylabel('Area (Km$^2$)') 
    plt.xscale('log')
    plt.yscale('log')
    plotnums=plotnums+1

    plt.show()

2 个答案:

答案 0 :(得分:1)

我建议在循环中加载数据。此外,您应该捕获变量中的轴控制柄,以控制用于绘制数据的轴。为了避免任何数据工件,我建议在每次迭代结束时将变量设置为None

import numpy as np 
import matplotlib.pyplot as plt

# store your file names in a list to be able to iterate over them:

FILES = ['Gray1961.txt','Hack1957.txt','Rignon1996.txt',\
         'Robert1990.txt','Langbein1947_p145.txt','Langbein1947_p146.txt',\
         'Langbein1947_p149.txt','Langbein1947_p152.txt']

# specify desired conversion factors for each file, separated by x and y

xFactor =[1.00, 1.00, 1.00, 1.00\
          2.59, 2.59, 2.59, 2.59]

yFactor = [1.000, 1.000, 1.000, 1.000\
           1.609, 1.609, 1.609, 1.609] 

# loop through all files;
# range(len(FILES)) returns a list of integers from 0 to 7 in this example

for n in range(len(FILES)):

    # load the data from each file:

    RiverData = np.loadtext(FILES[n], skiprows = 2)

    # convert the data per the specified factors:

    X = [xi * xFactor[n] for xi in RiverData[:,0]]
    Y = [yi * yFactor[n] for yi in RiverData[:,1]]

    # create sub-plot, here, you need to use n+1,
    # because your loop iterable counts from 0,
    # but your sub-plots count from 1

    ax = plt.subplot(2,4,n+1)

    # use the created axis object to plot your data;
    # use ax.plot instead of plt.plot 

    ax.plot(X, Y,'ko') 

    # specify your axis details, again use ax.plot instead of plt.plot

    ax.set_xlabel('River Length (km)')
    ax.set_ylabel('Area (Km$^2$)') 

    # the file name can be used as plot title
    # (if you want to omit the .txt ending, use [:-4] 
    # to omit the last for characters in the title string)

    ax.set_title(FILES[n][:-4])
    ax.set_xscale('log')
    ax.set_yscale('log')

    # to avoid surprises going from one loop to the next,
    # clear the data from the variables

    RiverData = None
    ax = None

plt.show()

作为Thiru pointed out,您不需要在for - 循环内增加迭代。

答案 1 :(得分:0)

请查看子图上的matplotlib文档

http://matplotlib.org/examples/animation/subplots.html

您可以创建一个图形并为其添加多个子图。

fig = plt.figure()
for plotnums in range(1,9):
    plot1 = fig.add_subplot(2,4,plotnums) # update the numbers as required
    ...