Matplotlib's show function triggering unwanted output

Question:

Whenever I have any Python code executed via Python v3.10.4 with or without debugging in Visual Studio Code v1.74.2, I get output looking like the following in the Debug Console window in addition to the normal output of the code. Otherwise, all of my Python programs work correctly and as intended at this time.

1   HIToolbox                           0x00007ff81116c0c2 _ZN15MenuBarInstance22RemoveAutoShowObserverEv + 30
2   HIToolbox                           0x00007ff8111837e3 SetMenuBarObscured + 115
3   HIToolbox                           0x00007ff81118a29e _ZN13HIApplication11FrontUILostEv + 34
4   HIToolbox                           0x00007ff811183622 _ZN13HIApplication15HandleActivatedEP14OpaqueEventRefhP15OpaqueWindowPtrh + 508
5   HIToolbox                           0x00007ff81117d950 _ZN13HIApplication13EventObserverEjP14OpaqueEventRefPv + 182
6   HIToolbox                           0x00007ff811145bd2 _NotifyEventLoopObservers + 153
7   HIToolbox                           0x00007ff81117d3e6 AcquireEventFromQueue + 494
8   HIToolbox                           0x00007ff81116c5a4 ReceiveNextEventCommon + 725
9   HIToolbox                           0x00007ff81116c2b3 _BlockUntilNextEventMatchingListInModeWithFilter + 70
10  AppKit                              0x00007ff80a973f33 _DPSNextEvent + 909
11  AppKit                              0x00007ff80a972db4 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1219
12  AppKit                              0x00007ff80a9653f7 -[NSApplication run] + 586
13  _macosx.cpython-310-darwin.so       0x0000000110407e22 show + 162
14  Python                              0x0000000100bb7595 cfunction_vectorcall_NOARGS + 101
15  Python                              0x0000000100c9101f call_function + 175
16  Python                              0x0000000100c8a2c4 _PyEval_EvalFrameDefault + 34676
17  Python                              0x0000000100c801df _PyEval_Vector + 383
18  Python                              0x0000000100c9101f call_function + 175
19  Python                              0x0000000100c8a2c4 _PyEval_EvalFrameDefault + 34676
20  Python                              0x0000000100c801df _PyEval_Vector + 383
21  Python                              0x0000000100b53f61 method_vectorcall + 481
22  Python                              0x0000000100c8a4f2 _PyEval_EvalFrameDefault + 35234
23  Python                              0x0000000100c801df _PyEval_Vector + 383
24  Python                              0x0000000100c9101f call_function + 175
25  Python                              0x0000000100c8a2c4 _PyEval_EvalFrameDefault + 34676
26  Python                              0x0000000100c801df _PyEval_Vector + 383
27  Python                              0x0000000100cf536d pyrun_file + 333
28  Python                              0x0000000100cf4b2d _PyRun_SimpleFileObject + 365
29  Python                              0x0000000100cf417f _PyRun_AnyFileObject + 143
30  Python                              0x0000000100d20047 pymain_run_file_obj + 199
31  Python                              0x0000000100d1f815 pymain_run_file + 85
32  Python                              0x0000000100d1ef9e pymain_run_python + 334
33  Python                              0x0000000100d1ee07 Py_RunMain + 23
34  Python                              0x0000000100d201e2 pymain_main + 50
35  Python                              0x0000000100d2048a Py_BytesMain + 42
36  dyld                                0x00007ff80741b310 start + 2432
  1. Why do these lines come out in the Debug Console window though there is nothing in any of my Python programs that would directly cause them to come out as far as I know?
  2. How are they helpful and how can they be used if needed?
  3. How can I prevent them from coming out by default?

I checked out the Visual Studio Code documentation on Python debugging but could not come across anything that would explain these lines. I am running Visual Studio Code on macOS Ventura v13.1.


UPDATE as of January 2, 2023

I have figured out that the unwanted output in my initial post is being triggered by the matplotlib.pyplot.show function in my Python programs. I get that output even when I run a program as simple as that below:

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [1, 2, 3]
plt.plot(x, y)

plt.show()

When I remove plt.show() from the code above, the 36-line unwanted output does not come out but then the graph is also not displayed. Again, other than the unwanted output, all of my Python programs with the show function appear to work correctly, including the graph display triggered by the show function. I have Matplotlib 3.5.2 installed on my Mac.

A very similar unwanted output comes out if I run the same program directly through the command line as (assume the Python program’s name is test.py):

python3 test.py

but not when I run test.py through IDLE, Python’s Integrated Development and Learning Environment, or the code in it from within a Jupyter notebook.

I can remove the show function from my Python programs to avoid the unwanted output but then the charts will not appear and I would prefer using the show function rather than a makeshift solution.


UPDATE as of January 4, 2023

I was suggested at a Matplotlib forum that this might somehow be a macOS Ventura v13.1 issue because similar issues have started being reported recently with different programs executed under macOS Ventura v13.1. One user has reported encountering a similar output with code using Tkinter and another while using a video player called mpv.

It is not implausible that the problem is also related with macOS Ventura v13.1 but I don’t know how and my questions remain.


UPDATE as of January 6, 2023

Upgraded Matplotlib to v3.6.2 but the unwanted output issue has not been resolved.


UPDATE as of January 8, 2023

Tried Matplotlib v3.6.2 along with Python v3.11.1. The unwanted output issue remains.


UPDATE as of January 15, 2023

Reported this issue as a bug to Matplotlib Developers on GitHub: "[Bug]: Show function triggering unwanted additional output #24997
"


UPDATE as of January 16, 2023

I have found out that the unwanted output comes out only when the "Automatically hide and show the menu bar" option under Systems Settings->Desktop&Dock->Menu Bar is set to Always (which is my setting) or on Desktop Only. The unwanted output does not come out if I set that option to In Full Screen Only or Never.


UPDATE as of January 18, 2023

Both Matplotlib and Python developers on GitHub think that the unwanted output, which they can reproduce, is the result of a bug in macOS Ventura 13.1 and therefore there is not anything they can do about it.

For details, see the respective discussions following the bug report I mentioned submitting for Matplotlib on GitHub and also the one I later submitted for Tkinter through Python/CPython on GitHub again as "Tkinter causing unwanted output in most recent macOS". I was also told in response to the latter that a Feedback Assistant report had now been submitted to Apple about the identified bug.


UPDATE as of January 25, 2023

Upgraded the macOS on my Mac to Ventura 13.2 today (and further to Ventura 13.2.1 when it came out in mid-February). No change except that the unwanted output, when the small program is run, is now considerably longer (85 lines). As before, the program works fine otherwise and the unwanted output does not come out if I change my Mac’s menu bar setting, for example, to Never.

Asked By: Alper

||

Answers:

After looking a bit, it’s seems to be related to the Apple menu bar (a.k.a top bar).

" Workaround "

In your mac settings, set "Automatically hide and show the menu bar" to Never to avoid getting these stack trace logs.

> Issue present:

Mac Settings, "Automatically hide and show the menu bar"

> Issue not present:

Mac Settings, Value set to "Never"

Conclusion

I know this isn’t a solution, but until Apple fix the HIToolbox framework issue that cause this situation you have something to avoid getting polluted by unwanted stack traces.

Answered By: Ben Souchet

[workaround]

If you run python code (from a CLI) with matplotlib’s plt.show(), then the following will redirect the console/stderr output you (and I) are seeing.

  • redirect to a file for inspection: python plot_trajectory_3d.py 2>stderr.txt
  • redirect to nowhere: python plot_trajectory_3d.py 2>/dev/null

You may be able to get the IDE you use to run python with this additional redirect statement, too.

More info on redirect in these SO questions.

Perhaps obvious: import matplotlib.pyplot as plt

For reference, I am using macOS and these package versions:

- python      3.10.9
- matplotlib  3.7.0
Answered By: TransferOrbit

This problem appears to be caused by one or more bugs in macOS 13.1 and 13.2. It can be fully resolved only by downgrading macOS to 13.0 or earlier or upgrading it to 13.3 or later.

Answered By: Alper