2016-03-30 111 views
0

所以我一直在嘗試將文本文件加載到多個子圖上,但圖形總是顯示爲一個文本文件。任何人都可以將我指向正確的方向,以便如何去做這件事?從一個循環中的多個文本文件創建子圖

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() 
+0

去掉for循環中的'plotnums = plotnums + 1' – Thiru

回答

0

請檢查次要情節

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

的matplotlib文檔,您可以創建一個人物,次要情節多添加到它。

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

我可以繪製出8個子圖單獨顯示。問題是通過循環將每個text_file分配給8個單獨的子圖中的一個。 – Milo

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 -loop內的迭代。

+0

謝謝Schorsch,非常大的幫助。有沒有辦法按順序在每個子圖上方繪製不同的文件名,因爲它們都顯示爲Gray1961。 – Milo

+0

謝謝Schorsch,非常大的幫助。我有一個問題,每個文件由x(km^2區域)和y(長度= km)組成,前4個文件就是這種情況。然而,最後四個文件(landgbein文件)區域是(英里^ 2),長度是(英里)。我試圖乘以換算係數(x2.59英里^ 2到km^2)和(x1.609英里到km)。但還沒有能夠在你之前描述的循環中得到這個工作。有沒有辦法按順序在每個子圖上方繪製不同的文件名,因爲它們都與Gray1961一起出現! – Milo

+0

@Milo查看更改的代碼塊。如果這個答案對你有幫助,可以考慮通過點擊答案左上角的三角形('1'上方)來提升它。 [你甚至可以考慮接受它,通過點擊'1'下面的複選標記](http://stackoverflow.com/help/someone-answers)。 – Schorsch

相關問題