2016-03-30 8 views
0

Also habe ich versucht, Textdateien auf mehrere Unterplots zu laden, aber die Plots scheinen immer als eine Textdatei zu kommen. Kann mir jemand in die richtige Richtung zeigen, wie ich das anstellen soll?Unterpläne aus mehreren Textdateien in einer Schleife erstellen

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

Entfernen Sie die 'plotnums = plotnums + 1' in der for-Schleife – Thiru

Antwort

0

Bitte überprüfen Sie die matplotlib Dokumentation auf Nebenhandlungen

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

Sie eine Figur erstellen und mehrere Handlungsstränge, um es hinzuzufügen.

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

Ich bin in der Lage, die 8 Subplots fein einzeln zu plotten. Das Problem besteht darin, jede text_file über eine Schleife einem der 8 einzelnen Subplots zuzuweisen. – Milo

1

Ich empfehle, die Daten auch innerhalb der Schleife zu laden. Darüber hinaus sollten Sie das Achsenhandle in einer Variablen erfassen, um zu steuern, welche Achse zum Zeichnen der Daten verwendet wird. Um jegliche Datenartefakte zu vermeiden, empfehle ich, die Variablen am Ende jeder Iteration auf None zu setzen.

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

Wie Thiru pointed out Sie brauchen nicht zu erhöhen Ihre iterable in einem for -loop.

+0

Danke Schorsch, sehr große Hilfe. Gibt es eine Möglichkeit, die verschiedenen Dateinamen über jedem Teilplot in der Reihenfolge darzustellen, wie sie alle als Gray1961 erscheinen. – Milo

+0

Danke Schorsch, sehr große Hilfe. Ich habe das Problem, dass jede Datei aus einem x (Fläche in km^2) und y (Länge = km) besteht, was bei den ersten 4 Dateien der Fall ist. Die letzten vier Dateien (Landgbein-Dateien) befinden sich jedoch in (Meilen^2) und die Länge ist in (Meilen). Ich habe versucht, dies mit einem Umrechnungsfaktor zu multiplizieren (x2.59 für Meilen^2, um zu km^2 zu kommen) und (x1.609 für Meilen bis km). aber das war nicht in der Lage, das in der Schleife, die Sie zuvor beschrieben haben, zu funktionieren. Gibt es eine Möglichkeit, die verschiedenen Dateinamen über jedem Subplot der Reihe nach darzustellen, da sie alle immer mit Gray1961 erscheinen !! – Milo

+0

@Milo siehe geänderten Codeblock. Wenn diese Antwort Ihnen geholfen hat, ziehen Sie es in Betracht, indem Sie auf das obere Dreieck in der oberen linken Ecke der Antwort klicken (über der "1"). [Sie können es sogar in Erwägung ziehen, indem Sie auf das Häkchen unter der "1" klicken (http://stackoverflow.com/help/someone-answers). – Schorsch

Verwandte Themen