1
#!/usr/bin/env python

from pylab import *
from matplotlib.finance import candlestick
from datetime import datetime


data2 = [(datetime(2012, 2, 1), 3103, 3102.01, 3103.62, 3101.90),
      (datetime(2012, 2, 1), 3102, 3102.90, 3103.16, 3102.09),
      (datetime(2012, 2, 2), 3100.89, 3102.59, 3102.86, 3100.51),
      (datetime(2012, 2, 3), 3103.62, 3102.01, 3103.62, 3101.90),
      (datetime(2012, 2, 5), 3102.24, 3102.90, 3103.16, 3102.09),
      (datetime(2012, 2, 6), 3100.89, 3102.59, 3102.86, 3100.51)]

quotes = [(date2num(item[0]),) + item[1:] for item in data2]

fig = figure()
fig.subplots_adjust(bottom=0.2)
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)

candlestick(ax, quotes, width=0.6)

ax.xaxis_date()
ax.autoscale_view()
setp( gca().get_xticklabels(), rotation=45, horizontalalignment='right')

show()

这很好用并绘制了烛台图。这似乎与read_command_line(prog_args_array)

def read_command_line(prog_args_array):
    prog = prog_args_array.pop(0)
    parser = argparse.ArgumentParser(description=__doc__, prog=prog)
    parser.add_argument('--version', action='version', version=VERSION)  
    return parser.parse_args(prog_args_array)

def main(prog_args_array):
    args = read_command_line(prog_args_array)
    ...

if __name__ == '__main__':
    sys.exit(main(sys.argv))

这给了我这个:

Traceback (most recent call last):
  File "./tdseq4.py", line 124, in <module>
    sys.exit(main(sys.argv))
  File "./tdseq4.py", line 89, in main
    fig = figure()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1683, in __init__
    baseName = os.path.basename(sys.argv[0])
IndexError: list index out of range
make: *** [all] Error 1
4

2 回答 2

1

它不在您的数据中,而是在加载后端。如果你打电话

import matplotlib.pyplot as plt
print plt.get_backend()

它会告诉我你正在使用 Tkinter

您需要查看如何调用.py文件并从交互式 shell 中尝试。

因为 tkinter 只是在寻找 sys.argv[0] 你可以import sys;print sys.argv看看你的论点看起来如何

于 2012-07-11T15:53:14.980 回答
0

My guess is that you modify sys.argv, which causes problems. This can be solved with:

def read_command_line(prog_args_array):
    prog = prog_args_array[0]
    (…)

(instead of using pop(0), which in effect modifies sys.argv).

于 2012-07-12T04:54:13.907 回答