如何在同一图形上绘制不同的烛台图?

时间:2019-01-06 18:29:55

标签: python-3.x matplotlib

我想绘制烛台图并保存我绘制的每个图。为了提高速度,我想在同一图上执行此操作。 到目前为止,我可以保存第一个图像,但其余图像为空白。

我尝试使用:

pltclf()
plt.cla()
fig.clear()

我的代码:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ohlc
from itertools import count
colNames = ['Date', 'Time', 'Open', 'High', 'Low', 'Close']  
allData = pd.read_csv('file.csv', header=0, names=colNames, usecols=colNames,
                      parse_dates={'Datetime': ['Date', 'Time']},
                      infer_datetime_format=True)

fig, ax = plt.subplots()
for n in count(0, 10):
    subset = allData[n:n+10]
    plt.axis('off')
    try:
        candlestick2_ohlc(ax, subset['Open'], subset['High'], subset['Low'],
                          subset['Close'], width=0.6, colorup='g',
                          colordown='r', alpha=1)
    except ValueError:
        print("Graphing done.")
        break
    fig.savefig("{}.png".format(str(n)))
    plt.show()
    fig.clf()

使用的CSV文件(file.csv):

Date,Time,OpenBid,HighBid,LowBid,CloseBid
01/02/2009,09:31:00,1212.23,1212.29,1211.77,1211.77
01/02/2009,09:32:00,1211.53,1212.18,1211.29,1211.29
01/02/2009,09:33:00,1209.11,1209.91,1209.11,1209.91
01/02/2009,09:34:00,1210.3,1211.29,1210.3,1211.28
01/02/2009,09:35:00,1213.51,1214.51,1213.11,1213.11
01/02/2009,09:36:00,1212.79,1212.79,1212.56,1212.71
01/02/2009,09:37:00,1213.65,1214.33,1213.65,1213.75
01/02/2009,09:38:00,1213.39,1213.76,1213.39,1213.76
01/02/2009,09:39:00,1211.32,1213.17,1211.32,1213.17
01/02/2009,09:40:00,1213.92,1215.87,1213.92,1215.87
01/02/2009,09:41:00,1215.61,1216.1,1215.4,1216.1
01/02/2009,09:42:00,1215.74,1215.75,1214.07,1214.07
01/02/2009,09:43:00,1214.36,1214.88,1213.87,1214.88
01/02/2009,09:44:00,1215,1215,1213.35,1214.03
01/02/2009,09:45:00,1214.04,1214.44,1214.04,1214.12
01/02/2009,09:46:00,1214.16,1214.56,1214.16,1214.56
01/02/2009,09:47:00,1214.68,1214.93,1214.62,1214.68
01/02/2009,09:48:00,1215.59,1216.11,1215.59,1216.11
01/02/2009,09:49:00,1216.37,1216.37,1215.54,1215.61
01/02/2009,09:50:00,1215.29,1215.29,1214.28,1214.28
01/02/2009,09:51:00,1213.82,1213.82,1212.87,1212.99
01/02/2009,09:52:00,1212.25,1212.44,1212.21,1212.25
01/02/2009,09:53:00,1212.05,1212.05,1210.24,1210.24
01/02/2009,09:54:00,1210.05,1210.21,1209.27,1210.21
01/02/2009,09:55:00,1209.83,1210.68,1209.83,1209.99
01/02/2009,09:56:00,1209.97,1210.91,1209.97,1210.91
01/02/2009,09:57:00,1211.45,1212.62,1211.38,1212.17
01/02/2009,09:58:00,1212.42,1212.42,1212.26,1212.41
01/02/2009,09:59:00,1212.39,1212.39,1212.33,1212.33
01/02/2009,10:00:00,1212.28,1212.28,1211.26,1212.14
01/02/2009,10:01:00,1212.99,1214.66,1212.99,1214.19
01/02/2009,10:02:00,1213.91,1213.91,1213.16,1213.55
01/02/2009,10:03:00,1213.53,1213.53,1213.41,1213.46
01/02/2009,10:04:00,1212.67,1213.21,1212.54,1213.21
01/02/2009,10:05:00,1213.52,1213.81,1213.52,1213.52
01/02/2009,10:06:00,1213.34,1213.4,1213.29,1213.29
01/02/2009,10:07:00,1213.46,1213.6,1213.14,1213.14
01/02/2009,10:08:00,1213.37,1213.81,1213.37,1213.8
01/02/2009,10:09:00,1213.57,1214.5,1213.57,1214.45
01/02/2009,10:10:00,1214.93,1215.03,1214.62,1214.62

我认为问题与Candlestick2_ohlc有关,并且它绘制为“ ax”,但是我不知道如何解决我的问题。

1 个答案:

答案 0 :(得分:1)

这就是我要做的:

from mpl_finance import candlestick2_ohlc
from itertools import count
colNames = ['Date', 'Time', 'Open', 'High', 'Low', 'Close']  
allData = pd.read_csv(StringIO(d), header=0, names=colNames, usecols=colNames,
                      parse_dates={'Datetime': ['Date', 'Time']},
                      infer_datetime_format=True)

fig, ax = plt.subplots()
for n in count(0, 10):
    subset = allData[n:n+10]
    plt.axis('off')
    if len(subset)>0:
        candlestick2_ohlc(ax, subset['Open'], subset['High'], subset['Low'],
                          subset['Close'], width=0.6, colorup='g',
                          colordown='r', alpha=1)
        fig.savefig("{}.png".format(str(n)))
        ax.clear()
    else:
        break