0

So I have been trying to load text files onto multiple subplots but the plots always seem to come up as one text file. Can anyone point me into the right #direction as to how to go about this?

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()
1
  • remove the plotnums=plotnums+1 in the for loop Commented Mar 30, 2016 at 12:28

2 Answers 2

2

I suggest loading the data inside the loop as well. Additionally, you should capture the axis-handle in a variable to control which axis is used for plotting the data. To avoid any data artifacts, I suggest setting the variables to None at the end of each iteration.

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

As Thiru pointed out you do not need to increment your iterable inside a for-loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Schorsch, very big help. Is there a way to plot the different file names above each subplot in order, as they all appear as Gray1961.
Thanks Schorsch, very big help. I have an issue being that each file is composed of an x (area in km^2) and y(length=km), which is the case for the first 4 files. However the last four files (landgbein files) area is in (miles^2) and length is in (miles). Ive tried multiplying this by a conversion factor (x2.59 for miles^2 to get to km^2) and (x1.609 for miles to km). but have not been able to get this working in the loop you described before. Is there a way to plot the different file names above each subplot in order, as they all keep appearing with Gray1961!!
@Milo see changed code block. If this answer helped you, consider upvoting it, by clicking on the upper triangle at the upper left corner of the answer (above the 1). You can even consider accepting it, by clicking on the checkmark below the 1.
0

Please check the matplotlib documentation on subplots

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

You can create a figure and add multiple subplots to it.

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

1 Comment

I am able to plot the 8 subplots fine individually. The problem is assigning each text_file to one of the the 8 individual subplots, via a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.