为什么下一个直方图没有出现?

时间:2019-04-13 05:27:30

标签: python python-3.x matplotlib histogram matplotlib-widget

我正在使用按钮(上一个,下一个)绘制多个直方图。单击下一步时,下一个直方图不会出现。但是,会出现轴和标题。

我尝试使用plt.hist()而不是ax.hist(),但是随后单击下一步时,这些按钮将消失。 因此,为了使按钮出现,我尝试将按钮代码包含在if:else语句中。尽管按钮确实出现了,但是我无法使它们在语句内部起作用。

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




我希望这些图是一个很好的轮播。但是,仍在等待。

0 个答案:

没有答案