2

大家我是stackoverflow的新手!我正在尝试使用 python 绘制每小时 OHLC 烛台图表并从 CSV 文件加载。我的 CSV 文件包含 10000 行数据,我只想要特定日期范围内的数据,但日期量仍然无法放入单个图表中。因此,我试图通过Slider使用matplotlib.widgets. 这是我的 CSV 文件中数据的一部分,

 TimeStamp          Open    High    Low     Close
 11/7/2017 3:00     1.1395  1.1396  1.1394  1.1395
 11/7/2017 2:00     1.1394  1.1397  1.1394  1.1395
 11/7/2017 1:00     1.1396  1.1399  1.1393  1.1394
 11/7/2017 0:00     1.1398  1.1398  1.1396  1.1397
 10/7/2017 23:00    1.1398  1.1399  1.1397  1.1397
 10/7/2017 22:00    1.1399  1.1400  1.1395  1.1397
 10/7/2017 21:00    1.1401  1.1401  1.1397  1.1399
 10/7/2017 20:00    1.1401  1.1402  1.1399  1.1401
 10/7/2017 19:00    1.1404  1.1404  1.1399  1.1400
 10/7/2017 18:00    1.1407  1.1409  1.1404  1.1405
 10/7/2017 17:00    1.1397  1.1408  1.1397  1.1408
 10/7/2017 16:00    1.1397  1.1401  1.1396  1.1397
 10/7/2017 15:00    1.1389  1.1396  1.1386  1.1396
 10/7/2017 14:00    1.1393  1.1396  1.1389  1.139
 10/7/2017 13:00    1.1393  1.1394  1.1387  1.1391
 10/7/2017 12:00    1.139   1.1395  1.1386  1.1392
 10/7/2017 11:00    1.1392  1.1393  1.1384  1.139
 10/7/2017 10:00    1.1387  1.1395  1.1385  1.1395

以下是我的代码,

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
from matplotlib.finance import candlestick_ohlc
from matplotlib.widgets import Slider

eurusd = pd.read_csv('testing.csv')
eurusd = eurusd[(eurusd['TimeStamp'] >= '10/7/2017 11:00') & 
(eurusd['TimeStamp'] <= '11/7/2017 2:00')]

eurusd['TimeStamp'] = eurusd['TimeStamp'].map(lambda d: 
mdates.date2num(dt.datetime.strptime(d, '%d/%m/%Y %H:%M')))

fig, ax = plt.subplots()
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
plt.xticks(rotation = 45)
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("EUR/USD")

candlestick_ohlc(ax, 
eurusd[['TimeStamp','Open','High','Low','Close']].values, width = 0.01, 
colorup = 'g')

axcolor = 'lightgoldenrodyellow'
axpos = plt.axes([0.2, 0.05, 0.65, 0.03], facecolor = axcolor)

spos = Slider(axpos, 'Position', *eurusd.head(n = 1), *eurusd.tail(n = 1))

def update(val):
    pos = spos.val
    ax.axis([pos, pos + 10, 18.5, 22.5])
    fig.canvas.draw_idle()

spos.on_changed(update)

plt.show()

我尝试运行代码,但出现错误 - TypeError: __init__() takes from 5 to 12 positional arguments but 13 were given。如果有人可以帮助我运行我的代码,我将不胜感激。

4

0 回答 0