通过mpl_finance

时间:2018-10-07 22:35:54

标签: python matplotlib plot

使用以下代码,我可以成功绘制OHLC图

names = pd.Series(data.index.strftime("%d-%m-%y").unique())
indexs = pd.Series(data.index.date).unique()

ohlc = data[data.index.date == indexs[0]].copy()

ohlc['mdate'] = [mdates.date2num(d) for d in ohlc.index]

ohlc['SMA10'] = ohlc["Close"].rolling(10).mean()

fig, ax = plt.subplots(figsize = (10,5))
mpl_finance.candlestick2_ohlc(ax,ohlc['Open'],ohlc['High'],ohlc['Low'],ohlc['Close'], width = 0.6)
xdate = ohlc.index
def mydate(x, pos):
    try:
        return xdate[int(x)]
    except IndexError:
        return ''
ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate))

fig.autofmt_xdate()
fig.tight_layout()

plt.show()

但是,当我添加此行时
ax.plot(ohlc.mdate, ohlc["SMA10"], color ="green", label = "SMA50"), 我可以用两条垂直的细线绘制一个空图。请问这是怎么了?

enter image description here

                   Open    High     Low  ...    Volume          mdate  SMA10
Date_Time                                    ...                                
2018-02-13 11:55:00  7169.7  7172.4  7167.0  ...       444  736738.496528    NaN
2018-02-13 12:00:00  7171.6  7174.2  7164.2  ...       578  736738.500000    NaN
2018-02-13 12:05:00  7174.2  7174.7  7170.7  ...       458  736738.503472    NaN
2018-02-13 12:10:00  7172.0  7175.7  7171.2  ...       401  736738.506944    NaN
2018-02-13 12:15:00  7174.7  7176.7  7173.0  ...       389  736738.510417    NaN

这是我的数据列

Index(['Open', 'High', 'Low', 'Close', 'Volume', 'mdate', 'SMA10'], dtype='object')

1 个答案:

答案 0 :(得分:0)

替换

{"response":true,"ack_move":{"slot":3,"isWin":false,"isDraw":false,"row":[]}

作者

ax.plot(ohlc.mdate, ohlc["SMA10"], color ="green", label = "SMA50")