Skip to content

[Bug]: coredump when combining xkcd, FigureCanvasTkAgg and FuncAnimation #24908

Open
@bbbart

Description

@bbbart

Bug summary

When setting up a simple animation on a TkAgg canvas, everything works as expected, until the XKCD-style is applied.

Code for reproduction

import tkinter as tk                                                                
from datetime import date, datetime                                                 
from tkinter import ttk                                                             
                                                                                      
import matplotlib.animation as pltanim                                              
import matplotlib.pyplot as plt                                                     
from matplotlib.axes import Axes                                                    
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg                     
                                                                                      
                                                                                      
class DayPlotFrame(ttk.Frame):                                                      
    """TK Frame with matplotlib animation for live display of day progress."""   
                                                                                      
    def __init__(self, master=None):                                                
        """Initialize a new DayPlot."""                                             
        super().__init__(master=master)                                             
                                                                                    
        plt.xkcd()  # <- comment out this line and everything starts working again                                                                
        fig = plt.Figure()                                                          
        self.axes: Axes = fig.add_subplot()                                         
                                                                                    
        canvas = FigureCanvasTkAgg(fig, master=self)                                
        canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)                      
        self.animation = pltanim.FuncAnimation(                                     
            fig,                                                                    
            func=self.__run_animation,                                              
            init_func=self.__init_animation,                                        
        )               
        self.pack()                                                    
                                                                                       
    def __init_animation(self):                                                     
        """Configure the plot."""                                                   
        today = date.today()                                                        
                                                                                    
        self.axes.clear()                                                           
        self.axes.set_xlim(                                                         
            datetime(today.year, today.month, today.day, 8, 45, 0),                 
            datetime(today.year, today.month, today.day, 15, 11, 00),               
        )                                                                           
                                                                                      
    def __run_animation(self, frame):                                               
        """Update the plot according to the time of day."""                         
        width = datetime.now().replace(hour=10, minute=frame % 60)                  
        self.axes.barh(y=[1], width=width, color="blue")

ROOT = tk.Tk()
APP = DayPlotFrame(master=ROOT)
APP.mainloop()

Actual outcome

coredump

Expected outcome

the animation, but in XKCD style

Additional information

the init_func works just fine, the crash happens at the barh statement

Operating system

Arch

Matplotlib Version

3.6.2

Matplotlib Backend

TkAgg

Python version

Python 3.9.2

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions