I suspect I am using the interactive features of matplotlib
somehow in the wrong way, because I have to wait several seconds between one plot and the next when using the following code and structures.
My data base is a pandas
DataFrame
of around (3800 x 56) float
, with a 3-D hyerachical index.
df = mydf.set_index(['l1','l2','l3'])
I then go on creating the matplotlib
figure
and axes
fig = plt.figure()
ax = fig.add_subplot(111)
up_button_ax = fig.add_axes([0.25, 0.15, 0.1, 0.03], facecolor='grey')
up_button= Button(up_button_ax, 'pga_up')
dn_button_ax = fig.add_axes([0.25, 0.1, 0.1, 0.03], facecolor='grey')
dn_button= Button(dn_button_ax, 'pga_dn')
I struggled with the Slider
as I wanted it to take only integers, so I finally went for two buttons and had to use a small object to keep their state.
(not directly my question, but any suggestion whether there are other interactive elements which would avoid creation of this object would be welcome)
class Index(object):
ind = 0
def next(self):
self.ind += 1
def prev(self):
self.ind -= 1
callback = Index()
def up_button_on_clicked(mouse_event):
callback.next()
plot_data(callback.ind)
print('up i: {}'.format(callback.ind))
up_button.on_clicked(up_button_on_clicked)
def dn_button_on_clicked(mouse_event):
callback.prev()
plot_data(callback.ind)
print('dn i: {}'.format(callback.ind))
dn_button.on_clicked(dn_button_on_clicked)
def plot_data(i):
dfnow = df.xs(df.index.levels[0][i]) #slice by CMOD TUNE
ax.clear()
for c_pga in dfnow.index.levels[0]:
if not(dfnow.xs(c_pga).empty):
ax.plot(dfnow.xs(c_pga)['L1 (dBm)'],marker = 'o',markersize=1,label=c_pga)
ax.set_title('CMOD TUNE: {}'.format(df.index.levels[0][i]))
Upon execution, every time I click the button, I see the printout immediately, but have to wait quite a long time to have the data finally plotted.
If, on the other hand, I call plot_data(i)
from the shell, plotting is immediate.
I am therefore assuming that I am not calling the right drawing functions, but have no clue here.
EDIT
Inspired by a second read of Joe_Kington's reply on discrete Slider
(thanks to Tom for this), I thought of adding this simple line to my plot_data(i)
function:
fig.canvas.draw()
Drawing is now immediate. I would still be grateful if somebody would point me to the reasons why this is needed.