Tortar
Tortar

Reputation: 655

why doesn't plt.text show the textbox when I draw it?

I'm trying to create a graph with a text below it but I'm not able to figure out why the textbox bins_text is not showing. This is the code sample :

from matplotlib import pyplot as plt
from matplotlib.widgets import Button

results = [1,3,3,5]

class Index:
    
    def __init__(self):

        self.ind = 10
        
        self.draw_new_hist()

    def next(self, event):
        
        self.ind += 1
        
        self.remove_prev_hist()
        self.draw_new_hist()
        
    def prev(self, event):
        
        if self.ind > 1 : self.ind -= 1

        self.remove_prev_hist()
        self.draw_new_hist()
        
    def draw_new_hist(self):
        
        count, bins, self.bars = ax.hist(results, bins = self.ind, alpha = 0.5, color = "blue", ec = "black")
        ax.set_xlim([0.99*min(bins),1.01*max(bins)])
        ax.set_ylim([0,1.1*max(count)])
        
        self.bins_text = plt.text(-7,2,"number of bins : " + str(self.ind))

        plt.draw()

    def remove_prev_hist(self) :

        t = [b.remove() for b in self.bars]
        self.bins_text.remove()
        
fig, ax = plt.subplots()
plt.subplots_adjust(left = 0.1, bottom = 0.3)

callback = Index()

Box_button_1 = plt.axes([0.7, 0.05, 0.1, 0.075])
Box_button_2 = plt.axes([0.81, 0.05, 0.1, 0.075])

Button_prev = Button(ax = Box_button_1, label = 'Previous')
Button_prev.on_clicked(callback.prev)
Button_next = Button(ax = Box_button_2, label = 'Next')
Button_next.on_clicked(callback.next)

plt.show()

I tried to change the coordinates but without results, so I think the problem is something else, can someone help me please?

Edit :

Changing the coordinates with the first code sample was enough, here it doesn't work. The problem is that before I click any of the buttons the textbox is not showing but after I click one of them re-running the same code (since I call again self.draw_new_hist() ) the text 'magically' appears, but I can't figure out why.

Upvotes: 0

Views: 246

Answers (1)

Sean Walker
Sean Walker

Reputation: 108

I can make it work by modifying the coordinates. Example, try:

bins_text = plt.text(2,1,"number of bins : " + str(ind))

Remember that plt.txt uses data coordinates -- so you have to use the histogram bin (for x-axis) and histogram count (y-axis) to specify the location. Which is a bit error prone if you ask me!

With your updated code you can edit it like this to set self.bins_text to None so you know when you need to create the text and when you just need to modify the text using set_text():

from matplotlib import pyplot as plt
from matplotlib.widgets import Button


results = [1,3,3,5]

class Index:
    
    def __init__(self):

        self.ind = 10
        self.bins_text = None
        
        self.draw_new_hist()

    def next(self, event):
        
        self.ind += 1
        
        self.remove_prev_hist()
        self.draw_new_hist()
        
    def prev(self, event):
        
        if self.ind > 1 : self.ind -= 1

        self.remove_prev_hist()
        self.draw_new_hist()
        
    def draw_new_hist(self):
        
        count, bins, self.bars = ax.hist(results, bins = self.ind, alpha = 0.5, color = "blue", ec = "black")
        ax.set_xlim([0.99*min(bins),1.01*max(bins)])
        ax.set_ylim([0,1.1*max(count)])
        
        if self.bins_text is None:
            self.bins_text = plt.text(2,1,"number of bins : " + str(self.ind))
        else:
            self.bins_text.set_text("number of bins : " + str(self.ind))

        plt.draw()

    def remove_prev_hist(self) :

        t = [b.remove() for b in self.bars]
        #self.bins_text.remove()
        
fig, ax = plt.subplots()
plt.subplots_adjust(left = 0.1, bottom = 0.3)

callback = Index()

Box_button_1 = plt.axes([0.7, 0.05, 0.1, 0.075])
Box_button_2 = plt.axes([0.81, 0.05, 0.1, 0.075])

Button_prev = Button(ax = Box_button_1, label = 'Previous')
Button_prev.on_clicked(callback.prev)
Button_next = Button(ax = Box_button_2, label = 'Next')
Button_next.on_clicked(callback.next)

plt.show()

Upvotes: 1

Related Questions