画烛台(matplotlib)

时间:2012-07-11 14:44:03

标签: python python-3.x matplotlib

#!/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

2 个答案:

答案 0 :(得分:1)

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

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

它会显示我正在使用Tkinter

您需要了解如何调用.py文件并从交互式shell中进行尝试。

因为tkinter只查找sys.argv [0],你可以放import sys;print sys.argv来查看你的参数看起来如何

答案 1 :(得分:0)

我的猜测是你修改了sys.argv,这会导致问题。这可以通过以下方式解决:

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

(而不是使用pop(0),它实际上会修改sys.argv)。