最近,我专注于制作时间序列图。我通过使用pandas将所有csv数据读取到python,时间将是索引。
添加滚动条作为演示文稿的辅助工具。我从matplotlib.widgets导入Slider来生成它。而且一切看起来都不错。
但是,我想改进它。我现在制作的图表也不错。如果我想进入数据点,我可以单击滑块栏。
但是,是否可以添加一个按钮来控制滑块。 我的意思是我可以添加一个右键。单击此右键后,整个图形将向右移动5个数据点(或5分钟,因为它是时间序列模型) 我可以做一个'离开'按钮呢? 因此,如果我走得快,我可以直接点击滑块栏。那么,如果我想慢一点,我可以点击按钮吗?
实际上,我已经阅读了一些关于'按钮库'的信息。但是,我不知道如何使用它来更新Slider。
这是我的代码
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()
答案 0 :(得分:1)
您可以轻松地向地图添加matplotlib.widgets.Button
,方法与添加matplotlib.widgets.Slider
的方式相同。
然后,您可以将按钮连接到函数forward()
或backward()
,这会将滑块设置为新位置。
这是一个完整的工作示例,希望足够接近你的(我确实改变了一些东西)
import matplotlib.pyplot as plt
import matplotlib.dates
from matplotlib.widgets import Slider, Button
import datetime
import pandas as pd
import numpy as np
rng = pd.date_range(datetime.datetime.now(), periods=340, freq='T')
data = {'BidPrice': np.random.random(340)*np.sin(np.arange(340)/30.),
'AskPrice': np.random.random(340)*np.sin(np.arange(340)/30.-2) }
df_BA = pd.DataFrame(data, columns=['BidPrice','AskPrice'], index=rng)
ax = df_BA.plot(title= "Title", fontsize= 10, figsize=(10, 8))
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 = x[0]
x_max = x[0] + datetime.timedelta(minutes=20)
y_min = df_BA.BidPrice.min()
y_max = df_BA.AskPrice.max()
ax.set_xlim([x_min, x_max])
ax.set_ylim([y_min, y_max])
x_dt = datetime.timedelta(minutes=10)
axpos = plt.axes([0.2, 0.1, 0.58, 0.03], axisbg='w')
axpos1 = plt.axes([0.05, 0.1, 0.05, 0.03], axisbg='w')
axpos2 = plt.axes([0.93, 0.1, 0.05, 0.03], axisbg='w')
# Slider(axes, name, min, max)
spos = Slider(axpos, 'Pos', matplotlib.dates.date2num(x_min), matplotlib.dates.date2num(x[-1]))
spos.set_val(matplotlib.dates.date2num(x[0]))
button1 = Button(axpos1, '<', color='w', hovercolor='b')
button2 = Button(axpos2, '>', color='w', hovercolor='b')
def update(val):
pos = spos.val
xmin_time = matplotlib.dates.num2date(pos)
xmax_time = matplotlib.dates.num2date(pos) + x_dt
ax.set_xlim([xmin_time, xmax_time ])
plt.gcf().canvas.draw_idle()
def forward(vl):
pos = spos.val
spos.set_val(matplotlib.dates.date2num( matplotlib.dates.num2date(pos) + datetime.timedelta(minutes=5) ))
def backward(vl):
pos = spos.val
spos.set_val(matplotlib.dates.date2num( matplotlib.dates.num2date(pos) - datetime.timedelta(minutes=5) ))
spos.on_changed(update)
button1.on_clicked(backward)
button2.on_clicked(forward)
plt.show()