Recently, I am focusing on producing a time series graph. I read all the csv data to python by using pandas,which the time will be the index.
scrollbar is added as aid for presentation. I import Slider from matplotlib.widgets to produce it. And All looks fine.
However, I want to improve it. The graph now I produce is not bad. If I want to go into the datapoint I can just click the Slider bar.
However, is it possible to add a button to control the slider bar. What I mean is that can I add a right button. Once I click this right button, the whole graph will move right 5 datapoint( or 5 minutes as it is a time series model) And can I make a 'left' button too? Therefore, if I will go fast I can direct click the Slider bar. Then, If i wanna present it slow, I can just click the button?
Actually, I have read some information about 'button library'. However, I don't know how to use it to update the Slider.
Here is my code
df_BA = cs_66667_BS[['BidPrice','AskPrice']]
title_name = 'cs_66667_BS'
ax = df_BA.plot(title= title_name, fontsize= 10, figsize=(20, 10))
ax.set_xlabel('Date', fontsize= 15)
ax.set_ylabel('Price', fontsize= 15)
plt.subplots_adjust(left= 0.04, bottom= 0.25, right= 0.99, top= 0.95, wspace= 0, hspace= 0.1 )
x= df_BA.index.to_pydatetime()
x_min_index = 0
x_max_index = 1
x_min = x[0]
x_max = x[-1] - datetime.timedelta(minutes=4)
y_min = df_BA.BidPrice.min()
y_max = df_BA.AskPrice.max()
# timedelta
x_dt = datetime.timedelta(minutes=5)
axcolor = 'lightgoldenrodyellow'
axpos = plt.axes([0.05, 0.1, 0.9, 0.05], axisbg=axcolor)
slider_max = len(x) - x_max_index - 1
# Slider(axes, name, min, max)
spos = Slider(axpos, 'Pos', matplotlib.dates.date2num(x_min), matplotlib.dates.date2num(x_max))
# pretty date names
plt.gcf().autofmt_xdate()
def update(val):
pos = spos.val
xmin_time = matplotlib.dates.num2date(pos)
xmax_time = matplotlib.dates.num2date(pos) + x_dt
xmin_time = pos
ax.axis([xmin_time, xmax_time, y_min, y_max])
########################################################
fig.canvas.draw_idle()
spos.on_changed(update)
plt.show()