I am plotting multiple histograms using buttons(prev, next). On clicking on next, the next histogram does not appear. However, the axes and the title appear.
I have tried using plt.hist()
instead of ax.hist()
, But then the buttons would disappear on clicking next.
So then to make the buttons appear, I tried including the button codes inside of the if:else statements.Although the buttons did appear then, I could not make them work from inside the statements.
df = pd.read_excel('Book2.xlsm')
plt.ion()
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
axes.xaxis.set_major_locator(mticker.MaxNLocator(integer=True))
axes.xaxis.set_minor_locator(mticker.MultipleLocator(base=1.0))
ax.set_title("17 - Alpha OH PROGESTERONE - HORMONE ASSAYS" )
ax.hist(df.loc[0:1,'age':'age'].to_numpy(),4, rwidth=0.9)
class Index:
data = df
data_min = 0
data_max = data.shape[1]-1
selected = 0
def next(self, event):
global ax
global fig
if self.selected >=self.data_max:
self.selected = self.data_max
ax.set_title('Last sample reached. Cannot go forwards')
else:
self.selected += 1
if self.selected == 1:
plt.clf()
fig.subplots_adjust(bottom=0.2)
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
axes.xaxis.set_major_locator(mticker.MaxNLocator(integer=True))
axes.xaxis.set_minor_locator(mticker.MultipleLocator(base=1.0))
ax.set_title("17 ALPHA HYDROXY PROGESTERONE")
ax.hist(df.loc[2:6,'age':'age'].to_numpy(),4, rwidth=0.9)
elif self.selected == 2:
plt.clf()
fig.subplots_adjust(bottom=0.2)
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
axes.xaxis.set_major_locator(mticker.MaxNLocator(integer=True))
axes.xaxis.set_minor_locator(mticker.MultipleLocator(base=1.0))
ax.set_title("24 hrs URINE FOR CREATININE")
ax.hist(df.loc[7:17,'age':'age'].to_numpy(),4, rwidth=0.9)
def prev(self, event):
global ax
global fig
if self.selected >=self.data_max:
self.selected = self.data_max
ax.set_title('Last sample reached. Cannot go forwards')
else:
self.selected += 1
if self.selected == 1:
plt.clf()
fig.subplots_adjust(bottom=0.2)
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
axes.xaxis.set_major_locator(mticker.MaxNLocator(integer=True))
axes.xaxis.set_minor_locator(mticker.MultipleLocator(base=1.0))
ax.set_title("17 ALPHA HYDROXY PROGESTERONE")
ax.hist(df.loc[2:6,'age':'age'].to_numpy(),4, rwidth=0.9)
elif self.selected == 2:
plt.clf()
fig.subplots_adjust(bottom=0.2)
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
axes.xaxis.set_major_locator(mticker.MaxNLocator(integer=True))
axes.xaxis.set_minor_locator(mticker.MultipleLocator(base=1.0))
ax.set_title("24 hrs URINE FOR CREATININE")
ax.hist(df.loc[7:17,'age':'age'].to_numpy(),4, rwidth=0.9)
callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, '>')
bprev = Button(axprev, '<')
bnext.on_clicked(callback.next)
bprev.on_clicked(callback.prev)
plt.show()
I expect the graphs to be a nice carousel. However, still waiting.