1

我只是遇到了一个奇怪的问题。我编写代码的方式是,首先我在 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.
4

0 回答 0