python - 绘制烛台(matplotlib)

标签 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

最佳答案

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

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

它会告诉我你正在使用 Tkinter

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

由于 tkinter 仅查找 sys.argv[0],因此您可以输入 import sys;print sys.argv 来查看参数的外观

关于python - 绘制烛台(matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11435209/

相关文章:

python - 创建时向 django-rest-framework ModelViewSet 添加其他字段的最佳方法

python - 根据最相似的列和索引标签(与引用相比)查找 Pandas Dataframe 内的特定单元格

python - 运行 cap.sniff() 时出错,实时数据包 pyshark

python - "TypeError: object() takes no parameters"将 python2 元类转换为 python3

python - Matplotlib - 2 Figures in Subplot - 1 is Animation

python - 如何在 python 中为不同的条形图组赋予不同的颜色?

python - 消除 matplotlib 图中子图之间的空白

python - 列出给定 Azure 帐户的订阅

python - 如何在Python中按另一列的条件过滤时取一列的平均值

python - 尝试循环裁剪图像时出现 TypeError : NoneType,