多个文件的图上的回溯线 [英] Traceback lines on plot of multiple files

查看:30
本文介绍了多个文件的图上的回溯线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从多个文件中绘制数据.我不想使用 glob 模块,因为我需要分别绘制每个文件中的数据.数据正在绘制,但是当使用 Matplotlib 绘制它们时,图中有回溯"线.地块的图像如下:

I am plotting data from multiple files. I do not want to use the glob module since I need to plot the data from each file separately. The data is plotting, but there are 'traceback' lines on the plot when they are graphed using Matplotlib. The image of the plots is below:

这里有一些示例数据可帮助解决问题,并对缺少格式表示抱歉.数据来自未格式化的文本文件.如果将两个数据集分成两个单独的文件,则应重新创建问题.

Here are some sample data to help solve the problem and im sorry about the lack of formatting. The data is from unformatted text files. If you split the two data sets into two separate files it should recreate the issue.

Start-Mi, End-Mi,   IRI LWP, IRI R e
194.449,    194.549,    75.1,   92.3
194.549,    194.649,    85.2,   82.8
194.649,    194.749,    90.8,   91.8
194.749,    194.849,    79.3,   73.7
194.849,    194.949,    76.9,   80.1
194.949,    195.049,    82.7,   86.9
195.049,    195.149,    103,    116.7
195.149,    195.249,    81.5,   96.1
195.249,    195.349,    96.7,   92.7
195.349,    195.449,        59.5,   72.2

Start-Mi, End-Mi,   IRI LWP, IRI R e
194.449,    194.549,    79.9,   95.7
194.549,    194.649,    87.4,   96.5
194.649,    194.749,    86.5,   105.3
194.749,    194.849,    77, 76
194.849,    194.949,    73.6,   85.2
194.949,    195.049,    81.7,   94.3
195.049,    195.149,    104.6,  128.2
195.149,    195.249,    84.2,   98.6
195.249,    195.349,    94.2,   91.3
195.349,    195.449,    57.5,   72.1

当代码在新文件上开始新的数据绘图时,将创建回溯行.我试图摆脱从图的末端到起点的水平线.我需要清理绘图,因为代码旨在迭代无限数量的数据文件.代码如下所示:

The traceback lines are created when the code begins a new data plot on a new file. Im trying to get rid of the horizontal lines drawn from the end of the plot back to the beginning. I need clean up the plot since the code is designed to iterate over a indefinite number of data files. The code is shown below:

def graphWriterIRIandRut():
    n = 100
    m = 0
    startList = []
    endList = []
    iriRList = []
    iriLList = []
    fileList = []
    for file in os.listdir(os.getcwd()):
        fileList.append(file)
    while m < len(fileList):
        for col in csv.DictReader(open(fileList[m],'rU')):
            startList.append(float(col['Start-Mi']))
            endList.append(float(col['  End-Mi']))
            iriRList.append(float(col[' IRI R e']))
            iriLList.append(float(col['IRI LWP ']))

        plt.subplot(2, 1, 1)
        plt.grid(True)
        colors = np.random.rand(n)
        plt.ylabel('IRI value',fontsize=12)
        plt.title('Right IRI data per mile for 2016 calibrations: ')
        plt.plot(startList,iriRList,c=colors)
        plt.tick_params(axis='both', which='major', labelsize=8)

        plt.subplot(2, 1, 2)
        plt.grid(True)
        colors = np.random.rand(n)
        plt.ylabel('IRI value',fontsize=12)
        plt.title('Left IRI data per mile for 2016 calibrations: ')
        plt.plot(startList,iriLList,c=colors)
        plt.tick_params(axis='both', which='major', labelsize=8)

        m = m + 1
        continue

    plt.show()
    plt.gcf().clear()
    plt.close('all')

推荐答案

您的代码目前正在执行以下操作:

Your code is currently doing the following:

  1. 从文件中读取数据,并将其添加到列表中
  2. 绘制列表

该列表在任何时候都不会被清除,因此您不断地绘制列表并附加越来越多的数据,其中大部分数据都被一遍又一遍地绘制.这也是为什么所有线条都具有相同颜色的原因:它是您制作的最后一张图的颜色,它完全覆盖了之前的所有图并添加了一条线.

The list is not cleared at any point, so you keep plotting the list with more and more data appended to it, most of which is being plotted over and over. This is also why all your lines have the same color: it is the color of the last plot you made, which exactly covers all the previous plots and adds one more line.

碰巧的是,pyplot 有一个漂亮的 hold 功能,可让您确保在图形上绘制的任何附加图都不会覆盖旧图.您甚至不需要生成自己的颜色序列.pyplot 也会为您做到这一点.

As it happens, pyplot has a nifty hold function that lets you ensure that any additional plots you make on a figure won't overwrite the old ones. You don't even need to generate your own color sequence. pyplot will do that for you too.

虽然您的程序在功能上是健全的,但您的代码中也存在一些风格"问题,可以轻松更正.它们充其量是非 Pythonic 的,而在最坏的情况下实际上是有问题的:

While your program is functionally sound, there are also a few "stylistic" issues in your code that can be easily corrected. They are un-Pythonic at best and actually problematic at worst:

  1. 文件应在打开后关闭.在 with 关键字中使用的上下文管理器是此操作的标准方法.
  2. for 循环相比,有更好的方法来复制 os.listdir 的结果.事实上,您根本不需要复制列表.
  3. 如果您正在编写一个 while 循环,每次迭代都会增加一个索引,那么它应该是一个 for 循环.
  4. 您永远不需要在循环结束时使用 continue.这是隐含的.
  1. Files should be closed after being opened. A context manager used in the with keyword is the standard approach for this.
  2. There are better ways to copy the result of os.listdir than a for loop. In fact, you don't need to copy the list at all.
  3. If you are writing a while loop that increments an index on every iteration, it should be a for loop.
  4. You never need a continue at the end of a loop. It is implied.

因此,这里是结合以上所有内容的解决方案.此版本假定您不需要在绘制给定文件后保留其内容:

So here is a solution that combines all of the above. This version assumes that you do not need to keep the contents of a given file around after you plot it:

def graphWriterIRIandRut():
    # Set up the plots
    plt.subplot(2, 1, 1)
    plt.grid(True)
    plt.ylabel('IRI value', fontsize=12)
    plt.title('Right IRI data per mile for 2016 calibrations:')
    plt.tick_params(axis='both', which='major', labelsize=8)
    plt.hold(True)

    plt.subplot(2, 1, 2)
    plt.grid(True)
    plt.ylabel('IRI value', fontsize=12)
    plt.title('Left IRI data per mile for 2016 calibrations:')
    plt.tick_params(axis='both', which='major', labelsize=8)
    plt.hold(True)

    # Iterate over the files in the current directory
    for filename in os.listdir(os.getcwd()):
        # Initialize a new set of lists for each file
        startList = []
        endList = []
        iriRList = []
        iriLList = []

        # Load the file
        with open(filename, 'r') as file:
            for row in csv.DictReader(file):
                startList.append(float(row['Start-Mi']))
                endList.append(float(row[' End-Mi']))
                iriRList.append(float(row[' IRI R e']))
                iriLList.append(float(row['   IRI LWP']))

        # Add new data to the plots
        plt.subplot(2, 1, 1)
        plt.plot(startList, iriRList)
        plt.subplot(2, 1, 2)
        plt.plot(startList, iriLList)

    plt.show()
    plt.close('all')

在您提供的输入上运行此函数会产生下图:

Running this function on the inputs you provided yields the following figure:

为了更有效地处理 CSV 和表格数据,您可能需要查看 pandas库.这是一个非常强大的分析工具,其中包括您可能会想到的大多数用例的绘图和IO例程.

For a more efficient way to work with CSVs and tabular data in general, you may want to check out the pandas library. It is a really powerful tool for analysis which includes plotting and IO routines for most of the use-cases you can probably imagine.

这篇关于多个文件的图上的回溯线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆