Issue 1:
Bug report

Bug summary

Import matplotlib.pyplot fails on MacOSX

Code for reproduction

import matplotlib.pyplot as plt

Actual outcome

Traceback (most recent call last):
File "/Users/Apollo/PycharmProjects/CT_registration/main.py", line 114, in <module>
moving_npa=fixed(sitk.GetArrayViewFromImage(movingImage)))
File "/Users/Apollo/venv/lib/python2.7/site-packages/ipywidgets/widgets/interaction.py", line 531, in __call__
show_inline_matplotlib_plots()
File "/Users/Apollo/venv/lib/python2.7/site-packages/ipywidgets/widgets/interaction.py", line 52, in show_inline_matplotlib_plots
from ipykernel.pylab.backend_inline import flush_figures
File "/Users/Apollo/venv/lib/python2.7/site-packages/ipykernel/pylab/backend_inline.py", line 9, in <module>
from matplotlib.backends.backend_agg import new_figure_manager, FigureCanvasAgg # analysis: ignore
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 32, in <module>
from matplotlib.backend_bases import (
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 53, in <module>
from matplotlib import (
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/textpath.py", line 17, in <module>
import matplotlib.font_manager as font_manager
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1469, in <module>
_rebuild()
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1450, in _rebuild
fontManager = FontManager()
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1086, in __init__
self.ttflist = createFontList(self.ttffiles)
File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 587, in createFontList
font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes

Process finished with exit code 1

Expected outcome

Matplotlib version

Operating system: Mac OS X Lion 10.7.5

Matplotlib version: 2.2.2

Matplotlib backend: MacOSX

Python version: 2.7.1

Jupyter version: jupyter-client==5.2.3 jupyter-core==4.4.0

Other libraries: SimpleITK, ipywidgets

I installed matplotlib from source. I am on a virtual environment.

Issue 2:
Bug report

As originally reported in http://gnats.netbsd.org/53425 , with matplotlib version 2.2.2, attempting to import matplotlib.pyplot on a NetBSD system fails with:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/pyplot.py", line 31, in <module>
import matplotlib.colorbar
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
import matplotlib.contour as contour
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/contour.py", line 20, in <module>
import matplotlib.font_manager as font_manager
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1469, in <module>
_rebuild()
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1450, in _rebuild
fontManager = FontManager()
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1086, in __init__
self.ttflist = createFontList(self.ttffiles)
File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 587, in createFontList
font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes

I tracked down the cause of this to mpl_PyFile_Dup() attempting to call the "flush" method of a file object that is open only for reading. Python implements this by calling fflush(), and calling fflush() on a file descriptor not open for writing is treated as an error on BSD systems (but silently ignored on Linux). The fflush() call returns -1 with errno=EBADF, and this ultimately gets translated into the rather unhelpful error message "TypeError: First argument must be a path or file object reading bytes" show above.

The call chain leading to the error is PyFT2Font_init() -> convert_open_args() -> mpl_PyFile_Dup().

Code for reproduction

On a BSD system, run

python2.7 -c 'import matplotlib.pyplot'

Actual outcome

The stack trace shown above is printed.

Expected outcome

The command silently exits with a success status.

Matplotlib version

Operating system: NetBSD 7.1_STABLE

Matplotlib version: 2.2.2

Matplotlib backend (print(matplotlib.get_backend())): GTKAgg

Python version: 2.7.15

Jupyter version (if applicable): N/A

Other libraries: N/A

matplotlib was installed via pkgsrc.

I have a patch which I will attempt to submit as a pull request.