【问题标题】:why doesn't plt.text show the textbox when I draw it?为什么 plt.text 在我绘制时不显示文本框?
【发布时间】:2021-06-21 22:50:08
【问题描述】:

我正在尝试创建一个带有文本下方的图表,但我无法弄清楚为什么文本框 bins_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.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()

我尝试更改坐标但没有结果,所以我认为问题是别的,有人可以帮我吗?

编辑

用第一个代码示例更改坐标就足够了,在这里它不起作用。问题是,在我点击任何按钮之前,文本框没有显示,但是在我点击其中一个之后重新运行相同的代码(因为我再次调用self.draw_new_hist())文本“神奇地”出现了,但我不知道为什么。

【问题讨论】:

    标签: python python-3.x matplotlib text


    【解决方案1】:

    我可以通过修改坐标让它工作。示例,试试:

    bins_text = plt.text(2,1,"箱数:" + str(ind))

    记住 plt.txt 使用 数据坐标 -- 所以你必须使用直方图 bin(用于 x 轴)和直方图计数(y 轴)来指定位置。如果你问我,这有点容易出错!

    使用更新后的代码,您可以像这样编辑它,将 self.bins_text 设置为 None,这样您就知道何时需要创建文本以及何时只需要使用 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()
    

    【讨论】:

    • 是的,你是对的,那么问题很微妙,因为我只是拿了一部分代码,问题没有解决,将坐标更改为 2,1
    • 哇,真的吗?你还没有看到吗?有趣...我不确定发生了什么。我在答案中添加了一张图片——你能发布你看到的内容吗?
    • 是的,现在我更改了包括所有代码在内的所有代码,以便您可以弄清楚
    • 好的,现在运行它——如果我能重现,那么我可以提供帮助:)
    • 好的,我有一个解决方案——主要思想是更新文本而不是删除它,因为显然 matplotlib 不喜欢这样。我将在上面编辑我的答案以包含该代码
    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2010-12-12
    • 2011-06-19
    相关资源
    最近更新 更多