[英]My Python code isn't pulling all the information from the .csv excel file and I cannot figure out why
I'm pretty new to the coding scene and my boss has kinda just thrown me into the fire, so I'm sorry if this is some rookie mistake, though I don't think it is. 我对编码领域还很陌生,而我的老板也把我惹上火了,所以如果这是菜鸟的错误,我很抱歉,尽管我认为不是。
So I'm trying to graph G versus l1 (that's not an eleven, but an L1). 因此,我尝试绘制G与l1的关系图(这不是11,而是L1)。 The data is in the file that I loaded from an excel file.
数据在我从excel文件加载的文件中。 The excel file is 14x250 so there are 14 arguments, each with 250 data points.
excel文件为14x250,因此有14个参数,每个参数包含250个数据点。 The graph I put up is formatted correctly, but zero y-values show up.
我放置的图形格式正确,但是显示了零个y值。 From what I can tell, the code is graphing the first 12 columns and leaving the last 2 columns (which contain the two sets of 'y' data) out.
据我所知,该代码绘制了前12列的图形,而省略了后2列(其中包含两组“ y”数据)。 I cannot find where the error is and it's driving me crazy!
我找不到错误所在,这使我发疯! ANY help would be greatly appreciated!
任何帮助将不胜感激!
header = ['l1', 'l2', 'l3', 'l4', 'l5', 'EI',
'S', 'P_right', 'P1_0', 'P3_0',
'w_left', 'w_right', 'G_left', 'G_right']
def loadfile(filename, skip=[], *args):
output = []
with open(filename, *args) as f:
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
for i, row in enumerate(reader):
if not(i in skip):
output.append(row)
return np.array(output)
outputs_l1 = [loadfile('FILE.csv'.format(p)) for p in p3_arr]
fig = plt.figure()
for output, col in zip(outputs_l1, colors):
plt.plot(output[:,0], output[:,10]*1E3, col+'-')
plt.legend(['$P3 = {} Pa$'.format(p) for p in p3_arr], loc=(1.05, 0.6), fontsize=16)
for output, col in zip(outputs_l1, colors):
plt.plot(output[:,0], output[:,11]*1E3, col+'--')
plt.ticklabel_format(axis='both', style='plain', scilimits=(-1,1))
plt.xlabel('$l1 (m)$')
plt.ylabel('G $(J / m^2) * 10^{-3}$')
plt.xlim(xmin=.2)
plt.ylim(ymax=2, ymin=0)
plt.subplots_adjust(top=0.8, bottom=0.15, right=0.7)
Using column names instead of numbers and combining the two plotting loops, 使用列名代替数字,并结合两个绘图循环,
col = {name:i for i,name in enumerate(header)}
fig = plt.figure()
for data,color in zip(outputs_l1, colors):
xs = data[:, col["l1" ]]
wl = data[:, col["w_left" ]] * 1000.0 # column 10
wr = data[:, col["w_right"]] * 1000.0 # column 11
plt.plot(xs, wl, color + "-", wr, color + "--")
a possible error becomes clear: you say you want to graph G
, but you are specifying w
instead (cols 10 and 11, instead of cols 12 and 13). 一个可能的错误变得很明显:您说要绘制
G
,但您指定的是w
(列10和11,而不是列12和13)。
Maybe w_left
and w_data
do not appear because they are outside your specified limits ( plt.ylim(ymax=2, ymin=0)
)? 也许
w_left
和w_data
不会出现,因为它们超出了您指定的限制( plt.ylim(ymax=2, ymin=0)
)?
I think what you actually want is 我想你真正想要的是
fig = plt.figure()
for data,color in zip(outputs_l1, colors):
xs = data[:, col["l1" ]]
gl = data[:, col["G_left" ]] * 1000.0 # column 12
gr = data[:, col["G_right"]] * 1000.0 # column 13
plt.plot(xs, gl, color + "-", gr, color + "--")
In 在
def loadfile(filename, skip=[], *args):
passing []
as a default argument is a bit dangerous because changes to the default list can persist across calls to the function, causing very strange results. 将
[]
作为默认参数传递会有些危险,因为对默认列表的更改会在函数调用期间持续存在,从而导致非常奇怪的结果。 You should instead do 你应该做
def loadfile(filename, skip=None, *args):
skip = skip or []
except you are only using skip for membership-testing, so it would be faster as a set, so it becomes 除非您只使用skip进行成员资格测试,所以作为一个集合它会更快,所以它成为
def loadfile(filename, skip=None, *args):
skip = set(skip or [])
Also, 也,
output = []
with open(filename, *args) as f:
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
for i, row in enumerate(reader):
if not(i in skip):
output.append(row)
return np.array(output)
could be shortened to 可以缩短为
with open(filename, *args) as f:
cr = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
return np.array(row for i,row in enumerate(cr) if i not in skip)
and the result is a numpy 2d array; 结果是一个numpy 2d数组; so
所以
outputs_l1 = [loadfile('FILE.csv'.format(p)) for p in p3_arr]
looks like p3_arr
is a list determining which data-sets you want to look at; 看起来
p3_arr
是确定要查看哪些数据集的列表; then outputs_l1
becomes a list of 2d arrays. 然后
outputs_l1
成为二维数组的列表。 This is a misleading name because, looking at header
, l1
is supposed to be the first column of each 2d array; 这是一个误导性的名称,因为从
header
来看, l1
应该是每个2d数组的第一列; thus outputs_l1
logically ought to be a 2d array containing just first-column data. 因此,
outputs_l1
逻辑上应该是仅包含第一列数据的2d数组。 I suggest renaming it to something like p_data
. 我建议将其重命名为
p_data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.