【问题标题】:Creating subplots from multiple text files in a loop在循环中从多个文本文件创建子图
【发布时间】:2016-07-18 10:23:26
【问题描述】:

所以我一直在尝试将文本文件加载到多个子图上,但这些图似乎总是作为一个文本文件出现。谁能指出我正确的#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()

【问题讨论】:

  • 删除for循环中的plotnums=plotnums+1

标签: matplotlib python-3.5 subplot


【解决方案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 中增加你的迭代。

【讨论】:

  • 感谢 Schorsch,非常有帮助。有没有办法按顺序在每个子图上方绘制不同的文件名,因为它们都显示为 Gray1961。
  • 感谢 Schorsch,非常有帮助。我有一个问题是每个文件都由一个 x(以 km^2 为单位的面积)和 y(长度=km)组成,前 4 个文件就是这种情况。然而,最后四个文件(landgbein 文件)区域为(英里^2),长度为(英里)。我尝试将其乘以转换因子(x2.59 英里 ^ 2 到 km ^ 2)和(x1.609 英里到公里)。但无法在您之前描述的循环中完成此工作。有没有办法按顺序在每个子图上方绘制不同的文件名,因为它们都以 Gray1961 出现!
  • @Milo 查看更改的代码块。如果此答案对您有所帮助,请考虑通过单击答案左上角的上三角(1 上方)来支持它。 You can even consider accepting it, by clicking on the checkmark below the 1.
【解决方案2】:

请查看关于子图的 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
    ...

【讨论】:

  • 我可以单独绘制 8 个子图。问题是通过循环将每个 text_file 分配给 8 个单独的子图之一。
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多