0

I've been collecting data from experiments and dumping them into .txt files and I decided to try and write a quick script to plot them with matplotlib. The plotting works fine, but I do not know how to label the plot based on the file name.

from numpy import *
from pylab import *
from matplotlib import rc
import sys
import os

rc('text',usetex=True)
rc('font',**{'family':'serif','serif':['Computer Modern']})


os.chdir(".")
for files in os.listdir("."):
    if files.endswith(".txt"):
        f = open(files,'r')
        temp = []
        for l in f:
            temp.append(float(l))
        plot(temp,labels=files)
        hold(True)


legend(loc='lower left')
hold(False)

# Save the figure in a separate file
savefig('test.png')

# Draw the plot to the screen
show()

The problem seems to be with plot(temp,lables=files). If I put lables=files I get the error TypeError: There is no line property "labels". If I try and put labels='files', all the plots are labelled files which is useless. Does anyone know how to assign a lable to a plot based on a variable?

1
  • Do not use syntax from foo import *. Just do not. Commented Nov 8, 2013 at 21:37

1 Answer 1

2

You want to use label, not lables or labels.

plot(temp,label = files)
Sign up to request clarification or add additional context in comments.

2 Comments

I just tried that and got the error RuntimeError: LaTeX was not able to process the following string: 'Reference_9.txt'
You can just add a string replace line in your for loop, e.g. plot( temp, labels = files.replace('_', '-') )

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.