2

everyone I am new to stackoverflow! I am trying to plot an hourly OHLC candlestick chart using python and loading from a CSV file. My CSV file contains 10000 rows of data and I only want data within specific date range but the amount of date still cannot fit in a single chart. Therefore, I am trying to do make the chart scrollable by making use of the Slider function from matplotlib.widgets. This is part of the data in my CSV file is,

 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

Below are my codes,

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()

I tried running the codes but I am getting the error - TypeError: __init__() takes from 5 to 12 positional arguments but 13 were given. I would really appreciate if someone can help me to get my codes running.

Denzel
  • 358
  • 5
  • 19
  • In the line `spos = Slider(axpos, 'Position', *eurusd.head(n = 1), *eurusd.tail(n = 1))` you are unpacking too many things with the star operator. – Bonifacio2 Jul 17 '17 at 16:25
  • I have tried `spos = Slider(axpos, 'Position', *eurusd['TimeStamp'].head(n = 1), *eurusd['TimeStamp'].tail(n = 1))` and the code is working but the slider does not seem to be working still. Whenever I click on any part of the slider it just goes to the max value. – Denzel Jul 19 '17 at 12:21

0 Answers0