Python 2.7 多个文件绘图上的回溯线
我正在从多个文件打印数据。我不想使用glob模块,因为我需要分别绘制每个文件中的数据。数据正在打印,但使用Matplotlib绘制图形时,在打印上有“回溯”线。图的图像如下所示: 下面是一些帮助解决这个问题的示例数据,我对缺少格式表示抱歉。数据来自未格式化的文本文件。如果将两个数据集拆分为两个单独的文件,则应重新创建问题Python 2.7 多个文件绘图上的回溯线,python-2.7,matplotlib,text,Python 2.7,Matplotlib,Text,我正在从多个文件打印数据。我不想使用glob模块,因为我需要分别绘制每个文件中的数据。数据正在打印,但使用Matplotlib绘制图形时,在打印上有“回溯”线。图的图像如下所示: 下面是一些帮助解决这个问题的示例数据,我对缺少格式表示抱歉。数据来自未格式化的文本文件。如果将两个数据集拆分为两个单独的文件,则应重新创建问题 Start-Mi, End-Mi, IRI LWP, IRI R e 194.449, 194.549, 75.1, 92.3 194.549, 1
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
及
当代码在新文件上开始新数据绘图时,将创建回溯线。我正试图摆脱从绘图结束回到开始的水平线。我需要清理绘图,因为代码被设计为迭代不确定数量的数据文件。代码如下所示:
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')
def graphwriteriandrut():
n=100
m=0
惊吓者=[]
endList=[]
iriRList=[]
iriLList=[]
文件列表=[]
对于os.listdir(os.getcwd())中的文件:
追加(文件)
当m
您的代码当前正在执行以下操作:
pyplot
有一个漂亮的hold
功能,可以确保在地物上绘制的任何其他绘图不会覆盖旧的绘图。您甚至不需要生成自己的颜色序列<代码>pyplot也可以为您完成此操作
虽然您的程序在功能上是合理的,但代码中也存在一些“风格”问题,这些问题很容易纠正。它们充其量不是蟒蛇式的,最坏的情况下实际上是有问题的:
中使用上下文管理器是实现这一点的标准方法
os.listdir
结果的方法比复制for
循环的方法更好。事实上,您根本不需要复制列表while
循环,在每次迭代中增加一个索引,那么它应该是for
循环continue
。这是隐含的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')
在您提供的输入上运行此函数将生成下图:
为了更有效地使用CSV和表格数据,您可能需要查看该库。它是一个非常强大的分析工具,其中包括您可能想象的大多数用例的绘图和IO例程。您的代码目前正在执行以下操作:
pyplot
有一个漂亮的hold
功能,可以确保在地物上绘制的任何其他绘图不会覆盖旧的绘图。您甚至不需要生成自己的颜色序列<代码>pyplot也可以为您完成此操作
虽然您的程序在功能上是合理的,但代码中也存在一些“风格”问题,这些问题很容易纠正。它们充其量不是蟒蛇式的,最坏的情况下实际上是有问题的:
中使用上下文管理器是实现这一点的标准方法
os.listdir
结果的方法比复制for
循环的方法更好。事实上,您根本不需要复制列表while
循环,在每次迭代中增加一个索引,那么它应该是for
循环continue
。这是隐含的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')
在您提供的输入上运行此函数将生成下图:
为了更有效地使用CSV和表格数据,您可能需要查看该库。它是一个非常强大的分析工具,包括绘图和IO例程