I am writing a unit test for a scientific function and when I was trying to check its shape
I obtained an error with matplotlib
.
I am using PyCharm Community Edition 2022.3.3, python 3.11, matplotlib 3.7.1 and PySide6 6.5.0 under Windows 10.
When debugging the test, I was trying to plot the function in console mode and I obtained this error/warning and no plot:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py", line 2812, in plot
return gca().plot(
^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py", line 2309, in gca
return gcf().gca()
^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py", line 906, in gcf
return figure()
^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\_api\deprecation.py", line 454, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py", line 840, in figure
manager = new_figure_manager(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py", line 384, in new_figure_manager
return _get_backend_mod().new_figure_manager(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py", line 3574, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py", line 3579, in new_figure_manager_given_figure
return cls.FigureCanvas.new_manager(figure, num)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py", line 1742, in new_manager
return cls.manager_class.create_with_canvas(cls, figure, num)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py", line 2858, in create_with_canvas
return cls(canvas_class(figure), num)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\backends\backend_qt.py", line 204, in __init__
_create_qApp()
File "C:\Users\devot\Work\PythonEnvs\science_env\Lib\site-packages\matplotlib\backends\backend_qt.py", line 134, in _create_qApp
QtWidgets.QApplication.setAttribute(
DeprecationWarning: Enum value 'Qt::ApplicationAttribute.AA_EnableHighDpiScaling' is marked as deprecated, please check the documentation for more information.
If I run the code not as a test everything is fine.
These are a working example to obtain the error:
# test_scientific_functions.py
import numpy as np
import matplotlib.pyplot as plt
def test_sin():
x = np.arange(0, 25, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
This code generates this message:
test\unit\test_scientific_functions.py:22 (test_sin)
def test_sin():
x = np.arange(0, 25, 0.1)
y = np.sin(x)
> plt.plot(x, y)
test\unit\test_scientific_functions.py:26:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py:2812: in plot
return gca().plot(
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py:2309: in gca
return gcf().gca()
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py:906: in gcf
return figure()
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\_api\deprecation.py:454: in wrapper
return func(*args, **kwargs)
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py:840: in figure
manager = new_figure_manager(
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\pyplot.py:384: in new_figure_manager
return _get_backend_mod().new_figure_manager(*args, **kwargs)
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py:3574: in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py:3579: in new_figure_manager_given_figure
return cls.FigureCanvas.new_manager(figure, num)
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py:1742: in new_manager
return cls.manager_class.create_with_canvas(cls, figure, num)
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\backend_bases.py:2858: in create_with_canvas
return cls(canvas_class(figure), num)
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\backends\backend_qt.py:204: in __init__
_create_qApp()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
@functools.lru_cache(1)
def _create_qApp():
app = QtWidgets.QApplication.instance()
# Create a new QApplication and configure it if none exists yet, as only
# one QApplication can exist at a time.
if app is None:
# display_is_valid returns False only if on Linux and neither X11
# nor Wayland display can be opened.
if not mpl._c_internal_utils.display_is_valid():
raise RuntimeError('Invalid DISPLAY variable')
# Check to make sure a QApplication from a different major version
# of Qt is not instantiated in the process
if QT_API in {'PyQt6', 'PySide6'}:
other_bindings = ('PyQt5', 'PySide2')
elif QT_API in {'PyQt5', 'PySide2'}:
other_bindings = ('PyQt6', 'PySide6')
else:
raise RuntimeError("Should never be here")
for binding in other_bindings:
mod = sys.modules.get(f'{binding}.QtWidgets')
if mod is not None and mod.QApplication.instance() is not None:
other_core = sys.modules.get(f'{binding}.QtCore')
_api.warn_external(
f'Matplotlib is using {QT_API} which wraps '
f'{QtCore.qVersion()} however an instantiated '
f'QApplication from {binding} which wraps '
f'{other_core.qVersion()} exists. Mixing Qt major '
'versions may not work as expected.'
)
break
try:
> QtWidgets.QApplication.setAttribute(
QtCore.Qt.AA_EnableHighDpiScaling)
E DeprecationWarning: Enum value 'Qt::ApplicationAttribute.AA_EnableHighDpiScaling' is marked as deprecated, please check the documentation for more information.
..\..\PythonEnvs\science_env\Lib\site-packages\matplotlib\backends\backend_qt.py:134: DeprecationWarning
If I run this other code everything is fine:
# plot.py
import numpy as np
import matplotlib.pyplot as plt
def plot_sin():
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
if __name__ == "__main__":
plot_sin()
Do it mean that, in some way, pytest
goes into conflict with matplotlib.pyplot
?
Do you have any advice?