Python 使用变量matplotlib绘制和标记数据
我一直在收集实验数据并将其转储到.txt文件中,我决定尝试编写一个快速脚本,用matplotlib绘制它们。绘图工作正常,但我不知道如何根据文件名标记绘图Python 使用变量matplotlib绘制和标记数据,python,matplotlib,Python,Matplotlib,我一直在收集实验数据并将其转储到.txt文件中,我决定尝试编写一个快速脚本,用matplotlib绘制它们。绘图工作正常,但我不知道如何根据文件名标记绘图 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']})
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()
问题似乎出在
绘图(临时,标签=文件)
上。如果我放置lables=files
我会得到错误TypeError:没有行属性“labels”
。如果我尝试放置labels='files'
,所有绘图都是标记文件,这是无用的。有人知道如何根据变量为绘图指定标签吗?您想使用标签
,而不是标签
或标签
plot(temp,label = files)
我刚刚尝试了一下,得到了错误
RuntimeError:LaTeX无法处理以下字符串:“Reference_9.txt”
您可以在for循环中添加一个字符串替换行,例如plot(temp,labels=files.replace(“”,“-”)
不要使用来自foo import*的语法。别这样。