Skip to content

Use contourpy for quad contour calculations #22567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ jobs:

# Install dependencies from PyPI.
python -m pip install --upgrade $PRE \
cycler fonttools kiwisolver numpy packaging pillow pyparsing \
contourpy>=1.0.1 cycler fonttools kiwisolver numpy packaging pillow pyparsing \
python-dateutil setuptools-scm \
-r requirements/testing/all.txt \
${{ matrix.extra-requirements }}
Expand Down
4 changes: 2 additions & 2 deletions doc/api/next_api_changes/behavior/22229-TAC.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AritistList proxies copy contents on iteration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ArtistList proxies copy contents on iteration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When iterating over the contents of the the dynamically generated proxy lists
for the Artist-type accessors (see :ref:`Behavioural API Changes 3.5 - Axes
Expand Down
13 changes: 13 additions & 0 deletions doc/api/next_api_changes/behavior/22567-IT.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
New algorithm keyword argument to contour and contourf
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The contouring functions `~matplotlib.axes.Axes.contour` and
`~matplotlib.axes.Axes.contourf` have a new keyword argument *algorithm* to
control which algorithm is used to calculate the contours. There is a choice
of four algorithms to use, and the default is to use ``algorithm='mpl2014'``
which is the same algorithm that Matplotlib has been using since 2014.

Other possible values of the *algorithm* keyword argument are ``'mpl2005'``,
``'serial'`` and ``'threaded'``; see the
`ContourPy documentation <https://contourpy.readthedocs.io>`_ for further
details.
27 changes: 27 additions & 0 deletions doc/users/next_whats_new/use_contourpy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
New external dependency ContourPy used for quad contour calculations
--------------------------------------------------------------------

Previously Matplotlib shipped its own C++ code for calculating the contours of
quad grids. Now the external library
`ContourPy <https://github.com/contourpy/contourpy>`_ is used instead. There
is a choice of four algorithms to use, controlled by the *algorithm* keyword
argument to the functions `~matplotlib.axes.Axes.contour` and
`~matplotlib.axes.Axes.contourf`. The default behaviour is to use
``algorithm='mpl2014'`` which is the same algorithm that Matplotlib has been
using since 2014.

See the `ContourPy documentation <https://contourpy.readthedocs.io>`_ for
further details of the different algorithms.

.. note::

Contour lines and polygons produced by ``algorithm='mpl2014'`` will be the
same as those produced before this change to within floating-point
tolerance. The exception is for duplicate points, i.e. contours containing
adjacent (x, y) points that are identical; previously the duplicate points
were removed, now they are kept. Contours affected by this will produce the
same visual output, but there will be a greater number of points in the
contours.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the differences in clabel need to be mentioned here or are those usually small enough not to matter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will mention it here.


The locations of contour labels obtained by using
`~matplotlib.axes.Axes.clabel` may also be different.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ channels:
- conda-forge
dependencies:
- cairocffi
- contourpy>=1.0.1
- cycler>=0.10.0
- fonttools>=4.22.0
- kiwisolver>=1.0.1
Expand Down
39 changes: 29 additions & 10 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ class QuadContourSet(ContourSet):
%(contour_set_attributes)s
"""

def _process_args(self, *args, corner_mask=None, **kwargs):
def _process_args(self, *args, corner_mask=None, algorithm=None, **kwargs):
"""
Process args and kwargs.
"""
Expand All @@ -1433,21 +1433,31 @@ def _process_args(self, *args, corner_mask=None, **kwargs):
contour_generator = args[0]._contour_generator
self._mins = args[0]._mins
self._maxs = args[0]._maxs
self._algorithm = args[0]._algorithm
else:
import matplotlib._contour as _contour
import contourpy

if algorithm is None:
algorithm = mpl.rcParams['contour.algorithm']
mpl.rcParams.validate["contour.algorithm"](algorithm)
self._algorithm = algorithm

if corner_mask is None:
corner_mask = mpl.rcParams['contour.corner_mask']
if self._algorithm == "mpl2005":
# mpl2005 does not support corner_mask=True so if not
# specifically requested then disable it.
corner_mask = False
else:
corner_mask = mpl.rcParams['contour.corner_mask']
self._corner_mask = corner_mask

x, y, z = self._contour_args(args, kwargs)

_mask = ma.getmask(z)
if _mask is ma.nomask or not _mask.any():
_mask = None

contour_generator = _contour.QuadContourGenerator(
x, y, z.filled(), _mask, self._corner_mask, self.nchunk)
contour_generator = contourpy.contour_generator(
x, y, z, name=self._algorithm, corner_mask=self._corner_mask,
line_type=contourpy.LineType.SeparateCode,
fill_type=contourpy.FillType.OuterCode,
chunk_size=self.nchunk)

t = self.get_transform()

Expand Down Expand Up @@ -1772,6 +1782,15 @@ def _initialize_x_y(self, z):
Hatching is supported in the PostScript, PDF, SVG and Agg
backends only.

algorithm : {'mpl2005', 'mpl2014', 'serial', 'threaded'}, optional
Which contouring algorithm to use to calculate the contour lines and
polygons. The algorithms are implemented in
`ContourPy <https://github.com/contourpy/contourpy>`_, consult the
`ContourPy documentation <https://contourpy.readthedocs.io>`_ for
further information.

The default is taken from :rc:`contour.algorithm`.

data : indexable object, optional
DATA_PARAMETER_PLACEHOLDER

Expand All @@ -1792,5 +1811,5 @@ def _initialize_x_y(self, z):
3. `.contour` and `.contourf` use a `marching squares
<https://en.wikipedia.org/wiki/Marching_squares>`_ algorithm to
compute contour locations. More information can be found in
the source ``src/_contour.h``.
`ContourPy documentation <https://contourpy.readthedocs.io>`_.
""")
3 changes: 2 additions & 1 deletion lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,11 @@
## * CONTOUR PLOTS *
## ***************************************************************************
#contour.negative_linestyle: dashed # string or on-off ink sequence
#contour.corner_mask: True # {True, False, legacy}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs to be in the API change as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for corner_mask='legacy' was deprecated in mpl 1.5 (doc/api/prev_api_changes/api_changes_1.5.0.rst) and the code removed in mpl 2.2 (doc/api/prev_api_changes/api_changes_2.2.0.rst). This evidently missed the code removal.

#contour.corner_mask: True # {True, False}
#contour.linewidth: None # {float, None} Size of the contour line
# widths. If set to None, it falls back to
# `line.linewidth`.
#contour.algorithm: mpl2014 # {mpl2005, mpl2014, serial, threaded}


## ***************************************************************************
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ def _convert_validator_spec(key, conv):
"contour.negative_linestyle": _validate_linestyle,
"contour.corner_mask": validate_bool,
"contour.linewidth": validate_float_or_None,
"contour.algorithm": ["mpl2005", "mpl2014", "serial", "threaded"],

# errorbar props
"errorbar.capsize": validate_float,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading