mpl_finance删除Candlestick Python上的空白日期

时间:2019-03-05 14:18:43

标签: python candlestick-chart

我正在使用DataFrame。我的数据用于烛台。 问题是我无法删除周末日期。我的意思是,我的代码显示了这一点: enter image description here

我正在寻找: enter image description here

这是我的代码:

import matplotlib.ticker as ticker
import pandas as pd  
import numpy as np
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
df = pd.read_csv('AAPL.csv')
df['Date'] = pd.to_datetime(df['Date'])
df["Date"] = df["Date"].apply(mdates.date2num)
dates = df['Date'].tolist()
ohlc = df[['Date', 'Open', 'High', 'Low','Close']]
f1, ax = plt.subplots(figsize = (12,6))
candlestick_ohlc(ax, ohlc.values, width=.5, colorup='green', colordown='red')
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.setp(ax.get_xticklabels(), rotation=70, fontsize=7)
close = df['Close'].values
plt.plot(dates,close, marker='o')
plt.show()

数据框:

Date,Open,High,Low,Close,Adj Close,Volume
2019-02-04,167.410004,171.660004,167.279999,171.250000,170.518677,31495500
2019-02-05,172.860001,175.080002,172.350006,174.179993,173.436157,36101600
2019-02-06,174.649994,175.570007,172.850006,174.240005,173.495911,28239600
2019-02-07,172.399994,173.940002,170.339996,170.940002,170.210007,31741700
2019-02-08,168.990005,170.660004,168.419998,170.410004,170.410004,23820000
2019-02-11,171.050003,171.210007,169.250000,169.429993,169.429993,20993400
2019-02-12,170.100006,171.000000,169.699997,170.889999,170.889999,22283500
2019-02-13,171.389999,172.479996,169.919998,170.179993,170.179993,22490200
2019-02-14,169.710007,171.259995,169.380005,170.800003,170.800003,21835700
2019-02-15,171.250000,171.699997,169.750000,170.419998,170.419998,24626800
2019-02-19,169.710007,171.440002,169.490005,170.929993,170.929993,18972800
2019-02-20,171.190002,173.320007,170.990005,172.029999,172.029999,26114400
2019-02-21,171.800003,172.369995,170.300003,171.059998,171.059998,17249700
2019-02-22,171.580002,173.000000,171.380005,172.970001,172.970001,18913200
2019-02-25,174.160004,175.869995,173.949997,174.229996,174.229996,21873400
2019-02-26,173.710007,175.300003,173.169998,174.330002,174.330002,17070200
2019-02-27,173.210007,175.000000,172.729996,174.869995,174.869995,27835400
2019-02-28,174.320007,174.910004,172.919998,173.149994,173.149994,28215400

4 个答案:

答案 0 :(得分:1)

这是“不够”的解决方案,但我可以为您提供一些建议。

只需使用

import mplfinance as mpf


mpf.plot(df, type='candle')

尽管我不能完全满意,但这会自动忽略情节中的非交易日,使我更开心。希望对您有帮助。

检查一下。

https://github.com/matplotlib/mplfinance#basic-usage

答案 1 :(得分:0)

您可以在处理之前从数据帧中对其进行切片

请检查此链接Remove non-business days rows from pandas dataframe

答案 2 :(得分:0)

请勿将日期/时间用作索引,而应使用蜡烛号作为索引。 那么您的数据将连续不断,并且您不会中断时间序列。 因此,请使用蜡烛号作为Index,以绘制数据时需要绘制日期而不是日期/时间 如果要使用日期/时间进行绘图,则需要使用一列,其中已将蜡烛的时间戳记放入绘图中并将其放入绘图中..但随后您将再次出现空白。

答案 3 :(得分:0)

尝试过滤数据框。

df = df[df.Open.notnull()]

将此添加到您的绘图中。

show_nontrading=False