Python 2.7 显示Matplotlib中多个数据集的图形中的文件名

Python 2.7 显示Matplotlib中多个数据集的图形中的文件名,python-2.7,matplotlib,datainputstream,Python 2.7,Matplotlib,Datainputstream,我有这段代码,它可以将txt文件中的任意数量的数据集绘制在一个图形中。它工作得很好,但我无法确定如何在Matplotlib图形图例中显示所有文件名。我可以得到最后一个在图例中显示的文件,但我只能得到这些。那么,如何才能让每个文件名都显示在图例中?您可以在Github中使用基线文件进行测试 在循环外生成图例,但确保实际标记绘图 def graphWriterIRI(): ... # Iterate over the files in the current directory

我有这段代码,它可以将txt文件中的任意数量的数据集绘制在一个图形中。它工作得很好,但我无法确定如何在Matplotlib图形图例中显示所有文件名。我可以得到最后一个在图例中显示的文件,但我只能得到这些。那么,如何才能让每个文件名都显示在图例中?您可以在Github中使用基线文件进行测试


在循环外生成图例,但确保实际标记绘图

def graphWriterIRI():
    ...
    # Iterate over the files in the current directory
    for filename in os.listdir(os.getcwd()):
        ...
        if filename.endswith('.TXT'):
            with open(filename, 'rU') as file:
                for row in csv.DictReader(file):
                    ...
            filenames.append(filename)


        plt.subplot(2, 1, 1)
        plt.plot(startList,iriRList, label=filename)

        plt.subplot(2, 1, 2)
        plt.plot(startList,iriLList, label=filename)


    plt.legend()
    plt.show()

@重要性不,似乎没有创建一个图例@Importance我得到的只是底部图形框右上角的一个小正方形,我没有看到你实际上根本没有标记你的图。更新了答案。这很有效,但最有效的方法是将plt.legend放置在子地块中。但我会给你答案。谢谢它可以工作,但有点不好,因为如果你有100个文件,你会创建100个图例,其中一个替换了上一个。但如果它不会减慢你的绘图速度,你肯定会这样做。
def graphWriterIRI():
    ...
    # Iterate over the files in the current directory
    for filename in os.listdir(os.getcwd()):
        ...
        if filename.endswith('.TXT'):
            with open(filename, 'rU') as file:
                for row in csv.DictReader(file):
                    ...
            filenames.append(filename)


        plt.subplot(2, 1, 1)
        plt.plot(startList,iriRList, label=filename)

        plt.subplot(2, 1, 2)
        plt.plot(startList,iriLList, label=filename)


    plt.legend()
    plt.show()