我正在尝试生成一个简单的绘图,它读取存储在文件中的x,y值,并使用不同的颜色绘制每行。以下是我的尝试。
## to run ctrl+shift+b
#print("hello world")
import csv
import itertools
import pylab
import numpy as np
import matplotlib.pyplot as plt
f = open('2016-09-09-22_25_A_initial-Hysteresis.txt', 'r')
f2 = open('2016-09-09-22_25_F_initial-Hysteresis.txt', 'r')
f3 = open('2016-09-09-22_25_K_initial-Hysteresis.txt', 'r')
f4 = open('2016-09-09-22_25_P_initial-Hysteresis.txt', 'r')
f5 = open('2016-09-09-22_25_U_initial-Hysteresis.txt', 'r')
f6 = open('2016-09-09-22_26_A_initial-Hysteresis.txt', 'r')
f7 = open('2016-09-09-22_26_F_initial-Hysteresis.txt', 'r')
f8 = open('2016-09-09-22_26_K_initial-Hysteresis.txt', 'r')
f9 = open('2016-09-09-22_26_P_initial-Hysteresis.txt', 'r')
f10 = open('2016-09-09-22_26_U_initial-Hysteresis.txt', 'r')
x = f.readlines()
x2 = f2.readlines()
x3 = f3.readlines()
x4 = f4.readlines()
x5 = f5.readlines()
x6 = f6.readlines()
x7 = f7.readlines()
x8 = f8.readlines()
x9 = f9.readlines()
x10 = f10.readlines()
vars = [x, x2, x3, x4, x5, x6, x7, x8, x9, x10]
colors = ['b', 'r', 'g', 'c']
cc = itertools.cycle(colors)
datatable = []
i = 0
for content in vars:
for line_num, line_content in enumerate(content):
data = line_content.split()
row = [[], [], [], []]
for index, num in enumerate(data, start = 0):
isappendable = False
try:
data_attempt = float(num)
index = int(index)
row[index] = data_attempt
if(line_num > 15):
isappendable = True
except ValueError:
data_attempt = 0
if(isappendable):
datatable.append(row)
i = i + 1
c = next(cc)
index = []
input = []
output = []
for n,x in enumerate(datatable):
input.append(x[0])
output.append(x[1])
num_str = str(i)
name = "DUT" + num_str
plt.plot(input, output, label = name)
plt.legend(loc='upper left')
plt.show()
这看起来非常接近我想要的(有10条线彼此显着重叠)但我遇到的问题是图例中的颜色似乎与图中的颜色不匹配。图例显示了我想要绘制的颜色但是线条都是相同的(即使我放大它们)。我该如何解决这个问题?谢谢
我看了一下你的代码,试着找出错误,并确定了一些你应该改变的事情。
with
上下文管理器将负责清理。vars
和input
是Python中的保留关键字,所以虽然你可以使用它们(并且它可能会起作用),但使用其他东西可能更好。enumerate
,就像您为其他循环所做的那样,而不是使用i
。start = 0
,因为这是默认值。int(index)
因为索引已经是int
了。x
和index
一样,这使得它们更难以跟踪。datatable
,它应该在循环中初始化。否则,正如@Bazingaa所说,你正在重复同样的事情。n
,但您从未使用过它。index
数组但从不使用它。num_str
变量不起作用。我在下面解决了以下一些问题:
import csv
import itertools
import pylab
import numpy as np
import matplotlib.pyplot as plt
colors = ['b', 'r', 'g', 'c']
cc = itertools.cycle(colors)
# List of filenames to read
filenames = ["2016-09-09-22_25_A_initial-Hysteresis.txt", "2016-09-09-22_25_F_initial-Hysteresis.txt", ...]
for i, filename in enumerate(filenames):
with open ("/path/to/filename/"+filename) as f:
content = f.readlines()
datatable = []
for line_num, line_content in enumerate(content):
data = line_content.split()
row = [[], [], [], []]
for index, num in enumerate(data):
isappendable = False
try:
data_attempt = float(num)
row[index] = data_attempt
if line_num > 15:
isappendable = True
except ValueError:
data_attempt = 0
if(isappendable):
datatable.append(row)
c = next(cc)
input = []
output = []
for x in datatable:
input.append(x[0])
output.append(x[1])
name = "DUT" + i
plt.plot(input, output, label = name)
plt.legend(loc='upper left')
plt.show()
希望这可以帮助!
看起来整个代码可以简化为几行。
import numpy as np
import matplotlib.pyplot as plt
# List of filenames to read
filenames = ["2016-09-09-22_25_A_initial-Hysteresis.txt",
"2016-09-09-22_25_F_initial-Hysteresis.txt",
# ...
"2016-09-09-22_26_U_initial-Hysteresis.txt"]
for i, filename in enumerate(filenames):
inp, outp = np.loadtxt(filename, usecols=(0,1), unpack=True)
if len(inp) < 15:
continue
plt.plot(inp, outp, label = "DUT" + i)
plt.legend(loc='upper left')
plt.show()