此代码的目的是绘制一个包含7列
的csv文件['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']
进入烛台图表OHLC。
我开始将日期时间解析为单一事物,然后将其定义为索引。
以下是代码:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
column_names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']
df = pd.read_csv(r"E:\Business\Stocks\AUDUSD60.csv", names=column_names,
parse_dates={'DateTime': ['Date', 'Time']},
index_col=[0], header=None, low_memory=False)
f1 = plt.subplot2grid((6, 4), (1, 0), rowspan=6, colspan=4, axisbg='#07000d')
candlestick_ohlc(f1, df.values, width=.6, colorup='#53c156', colordown='#ff1717')
f1.xaxis_date()
f1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))
plt.xticks(rotation=45)
plt.ylabel('AUDUSD60')
plt.xlabel('Date Hours:Minutes')
plt.show()
但我收到了这个错误:
Traceback (most recent call last):
File "E:/Tutorial/pan.py", line 16, in <module>
plt.xticks(rotation=45)
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 1633, in xticks
locs = ax.get_xticks()
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 3029, in get_xticks
return self.xaxis.get_ticklocs(minor=minor)
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1272, in get_ticklocs
return self.major.locator()
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1061, in __call__
self.refresh()
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1081, in refresh
dmin, dmax = self.viewlim_to_dt()
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\dates.py", line 839, in viewlim_to_dt
return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\dates.py", line 445, in num2date
return _from_ordinalf(x, tz)
File "C:\Users\sydgo\Anaconda3\lib\site-packages\matplotlib\dates.py", line 260, in _from_ordinalf
dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1