Error using matplotlib in obspy script

I updated my obspy as suggested in another question.

Now another of my scripts returns an error from matplotlib:

Traceback (most recent call last):
File “./getnPlot.py”, line 838, in
thisFig.set_size_inches(plotSize2[0]/100.0, plotSize2[1]/100.0)
File “/home/seisan/anaconda3/envs/obspy/lib/python3.8/site-packages/matplotlib/figure.py”, line 3065, in set_size_inches
manager.resize(*(size * self.dpi).astype(int))
File “/home/seisan/anaconda3/envs/obspy/lib/python3.8/site-packages/matplotlib/backends/_backend_tk.py”, line 546, in resize
self.canvas._tkcanvas.configure(width=width, height=height)
File “/home/seisan/anaconda3/envs/obspy/lib/python3.8/tkinter/init.py”, line 1646, in configure
return self._configure(‘configure’, cnf, kw)
File “/home/seisan/anaconda3/envs/obspy/lib/python3.8/tkinter/init.py”, line 1636, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name “.!canvas”

Any suggestions as to why this is happening?

Apologies if these are basic problems, but Python is really doing my head in (I’m a user, not a coder).

Potential Problem Areas:

  1. Thread Safety: Tkinter is not thread-safe and using it in non-main threads can lead to errors like this. Ensure that all GUI-related operations are being run in the main thread.
  2. Figure Closure: If you’re trying to modify a figure or canvas that has already been closed or destroyed, you may encounter errors. Ensure that you’re working with an active/alive figure.
  3. Library Versions: Ensure that your libraries (Matplotlib, Tkinter, etc.) are up-to-date, and check if there are any known issues with the versions you’re using.

Steps to Resolve:

  1. Update Libraries: Ensure that all your libraries, especially Matplotlib and Tkinter, are updated to the latest versions.
  2. Use plt.show(block=True): If you’re using plt.show(), consider using plt.show(block=True) to make sure Python waits for all figures to be closed before proceeding.
  3. Check Object Lifetimes: Ensure that you’re not trying to modify or update a figure/canvas that has been closed or destroyed.
  4. Inspect plotSize2: Ensure that the plotSize2 variable contains valid and expected values. Invalid sizes might cause issues when setting figure size.
  5. Environment Consistency: Ensure that the environment you’re running your script in is consistent and doesn’t have conflicting library versions.

I would recommend avoiding to mix very old with very new version of packages that interact with each other whenever possible. The original problem from that other post was mixing a recent Numpy with old obspy. It’s a lot of work for package developers to ensure it works with different versions of their dependencies, so usually tests happen against only fairly recent version of the dependencies (e.g. numpy dropped Python 3.8 support in April 2023). Here it might be a new matplotlib not playing well with old TK (just a guess since we don’t have version number info)?

When you have to update environments doing automated processing tasks, I would recommend (esp. since you are using anaconda anyway) just setting up a new environment with all up to date packages then make sure your workflow works with the new environment and then just switch out what environment you use.

Your problem just might go away updating the whole stack of python, numpy, matplotlib, obspy. (Code might have to get updated in places, depending on what you are doing)

Thanks. My problems come I think when I use other peoples scripts. I don’t fully understand the way of doing things in Python / obspy. I guess I need to spend some time understanding things more.

Thanks. The problem, i think, is that I’m including someone elses scripts. I need to spend more time thinking about how it all works.