我遇到了一个奇怪的问题。我编写代码的方式,首先是在Ipython Qt控制台中交互式编写函数,并根据需要进行调整。一旦我对结果感到满意,我将它们移动到py文件中,以便稍后使用这些函数。
所以,我写了一个函数,它应该在屏幕上绘制直方图。如果我第一次运行我的py脚本,然后使用所需的参数调用该函数,我收到一条错误消息,这是在这篇文章的最底部。如果我将功能代码复制并粘贴到qt控制台,然后按回车键,那么该功能在此之后可以正常工作。
为什么在将其粘贴到qt控制台后,此功能正常工作,但如果我在使用%run script.py
魔法运行脚本后直接调用它,则无法正常工作?
感谢您的任何想法!
这是我的功能:
def PlotFreqHist(w_hist_list, head, span, m):
'''
Generates a frequency distribution plot
'''
if head == 30.:
tr_cond = 'Normal'
else:
tr_cond = 'Congested'
histweights = np.zeros_like(w_hist_list[0]) + 1. / w_hist_list[0].size * 100
bmap = brewer2mpl.get_map('RdYlBu', 'diverging', 10)
colors = bmap.mpl_colors
mpl.rcParams['axes.color_cycle'] = colors
fig = plt.figure(figsize=(12,8))
ax = axes()
plt.xlim(0.0,1.2)
plt.ylim(0.0,6)
plt.xlabel('Uniform Load [kips/ft]')
plt.ylabel('Frequency [%]')
plt.title('Frequency Distribution\n Number of Simulations = %i Span Length = %.0fft Traffic Condition = %s' %(m, span, tr_cond))
ax.xaxis.set_major_locator(MultipleLocator(0.1))
ax.xaxis.set_minor_locator(MultipleLocator(0.02))
ax.yaxis.set_major_locator(MultipleLocator(1.0))
ax.yaxis.set_minor_locator(MultipleLocator(0.2))
plt.grid(b=True, which='major', linewidth=1.0)
plt.grid(b=True, which='minor')
for i in xrange(len(w_hist_list)):
hist(w_hist_list[i], bins = 200, range = (0,2), normed = 0, cumulative = 0, histtype = charttype, linewidth=2.0, alpha =0.5, weights = histweights)
plt.show()
以下是错误消息:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 779, in structured_traceback
records = _fixed_getinnerframes(etb, context, tb_offset)
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 245, in _fixed_getinnerframes
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
File "C:\Python27\lib\inspect.py", line 1043, in getinnerframes
framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
File "C:\Python27\lib\inspect.py", line 1007, in getframeinfo
lines, lnum = findsource(frame)
File "C:\Python27\lib\inspect.py", line 580, in findsource
if pat.match(lines[lnum]): break
IndexError: list index out of range
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Unfortunately, your original traceback can not be constructed.