Skip to content

Commit

Permalink
Trac #34693: Further support for matplotlib 3.6
Browse files Browse the repository at this point in the history
#34668 was focusing on making doctests pass but did not test
docbuilding.

The following plot in `src/sage/plot/plot.py`
{{{
    .. PLOT::

        g =
plot(2*x+1,(x,0,5),ticks=[[0,1,e,pi,sqrt(20)],2],tick_formatter="latex")
        sphinx_plot(g)
}}}
is causing the building of the documentation to fail horribly.
{{{
[plotting ]  from /home/portage/sci-mathematics/sage-doc-9999/work/sage-
doc-9999/src/doc/en/reference/plotting/sage/plot/plot.rst:
[plotting ] Traceback (most recent call last):
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/artist.py", line 1378, in
_get_tightbbox_for_layout_only
[plotting ]     return obj.get_tightbbox(*args, **{**kwargs,
"for_layout_only": True})
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1251, in get_tightbbox
[plotting ]     ticks_to_draw = self._update_ticks()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1198, in _update_ticks
[plotting ]     minor_locs = self.get_minorticklocs()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1431, in get_minorticklocs
[plotting ]     mask = np.isclose(tr_minor_locs[:, None],
tr_major_locs[None, :],
[plotting ]   File "<__array_function__ internals>", line 180, in
isclose
[plotting ]   File "/usr/lib/python3.10/site-
packages/numpy/core/numeric.py", line 2373, in isclose
[plotting ]     yfin = isfinite(y)
[plotting ] TypeError: ufunc 'isfinite' not supported for the input
types, and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
[plotting ] During handling of the above exception, another exception
occurred:
[plotting ] Traceback (most recent call last):
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/artist.py", line 1378, in
_get_tightbbox_for_layout_only
[plotting ]     return obj.get_tightbbox(*args, **{**kwargs,
"for_layout_only": True})
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axes/_base.py", line 4428, in get_tightbbox
[plotting ]     ba = martist._get_tightbbox_for_layout_only(axis,
renderer)
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/artist.py", line 1380, in
_get_tightbbox_for_layout_only
[plotting ]     return obj.get_tightbbox(*args, **kwargs)
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1251, in get_tightbbox
[plotting ]     ticks_to_draw = self._update_ticks()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1198, in _update_ticks
[plotting ]     minor_locs = self.get_minorticklocs()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1431, in get_minorticklocs
[plotting ]     mask = np.isclose(tr_minor_locs[:, None],
tr_major_locs[None, :],
[plotting ]   File "<__array_function__ internals>", line 180, in
isclose
[plotting ]   File "/usr/lib/python3.10/site-
packages/numpy/core/numeric.py", line 2373, in isclose
[plotting ]     yfin = isfinite(y)
[plotting ] TypeError: ufunc 'isfinite' not supported for the input
types, and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
[plotting ] During handling of the above exception, another exception
occurred:
[plotting ] Traceback (most recent call last):
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/artist.py", line 1378, in
_get_tightbbox_for_layout_only
[plotting ]     return obj.get_tightbbox(*args, **{**kwargs,
"for_layout_only": True})
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1251, in get_tightbbox
[plotting ]     ticks_to_draw = self._update_ticks()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1198, in _update_ticks
[plotting ]     minor_locs = self.get_minorticklocs()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1431, in get_minorticklocs
[plotting ]     mask = np.isclose(tr_minor_locs[:, None],
tr_major_locs[None, :],
[plotting ]   File "<__array_function__ internals>", line 180, in
isclose
[plotting ]   File "/usr/lib/python3.10/site-
packages/numpy/core/numeric.py", line 2373, in isclose
[plotting ]     yfin = isfinite(y)
[plotting ] TypeError: ufunc 'isfinite' not supported for the input
types, and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
[plotting ] During handling of the above exception, another exception
occurred:
[plotting ] Traceback (most recent call last):
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/sphinxext/plot_directive.py", line 515, in _run_code
[plotting ]     exec(code, ns)
[plotting ]   File "<string>", line 2, in <module>
[plotting ]   File "<string>", line 37, in sphinx_plot
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/figure.py", line 3448, in tight_layout
[plotting ]     engine.execute(self)
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/layout_engine.py", line 180, in execute
[plotting ]     kwargs = get_tight_layout_figure(
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/_tight_layout.py", line 305, in
get_tight_layout_figure
[plotting ]     kwargs = _auto_adjust_subplotpars(fig, renderer,
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/_tight_layout.py", line 82, in
_auto_adjust_subplotpars
[plotting ]     bb += [martist._get_tightbbox_for_layout_only(ax,
renderer)]
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/artist.py", line 1380, in
_get_tightbbox_for_layout_only
[plotting ]     return obj.get_tightbbox(*args, **kwargs)
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axes/_base.py", line 4428, in get_tightbbox
[plotting ]     ba = martist._get_tightbbox_for_layout_only(axis,
renderer)
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/artist.py", line 1380, in
_get_tightbbox_for_layout_only
[plotting ]     return obj.get_tightbbox(*args, **kwargs)
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1251, in get_tightbbox
[plotting ]     ticks_to_draw = self._update_ticks()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1198, in _update_ticks
[plotting ]     minor_locs = self.get_minorticklocs()
[plotting ]   File "/usr/lib/python3.10/site-
packages/matplotlib/axis.py", line 1431, in get_minorticklocs
[plotting ]     mask = np.isclose(tr_minor_locs[:, None],
tr_major_locs[None, :],
[plotting ]   File "<__array_function__ internals>", line 180, in
isclose
[plotting ]   File "/usr/lib/python3.10/site-
packages/numpy/core/numeric.py", line 2373, in isclose
[plotting ]     yfin = isfinite(y)
[plotting ] TypeError: ufunc 'isfinite' not supported for the input
types, and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
}}}
Tests of this plot were disabled in #34668 and now we need to remove the
plot. The whole example needs to be replaced by something that works.

We could not overcome the problem of symbolic values that numpy rejects,
but instead made a fix to circumvent numpy and revived the doctest and
the example.

URL: https://trac.sagemath.org/34693
Reported by: fbissey
Ticket author(s): Kwankyu Lee
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Dec 4, 2022
2 parents 7064a32 + 6458968 commit 949866d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/sage/plot/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
elif x_locator == []:
x_locator = NullLocator()
elif isinstance(x_locator, list):
x_locator = FixedLocator(x_locator)
x_locator = FixedLocator([float(x) for x in x_locator])
else: # x_locator is a number which can be made a float
from sage.functions.other import ceil, floor
if floor(xmax / x_locator) - ceil(xmin / x_locator) > 1:
Expand All @@ -2387,7 +2387,7 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
elif y_locator == []:
y_locator = NullLocator()
elif isinstance(y_locator, list):
y_locator = FixedLocator(y_locator)
y_locator = FixedLocator([float(y) for y in y_locator])
else: # y_locator is a number which can be made a float
from sage.functions.other import ceil, floor
if floor(ymax / y_locator) - ceil(ymin / y_locator) > 1:
Expand Down Expand Up @@ -2419,7 +2419,11 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
LogFormatterMathtext(base=base[0])(n, pos).replace(
"\\mathdefault", ""))
else:
x_formatter = FuncFormatter(lambda n, pos: '$%s$' % latex(n))
# circumvent the problem of symbolic tick values (trac #34693)
if isinstance(x_locator, FixedLocator):
x_formatter = FixedFormatter(['$%s$' % latex(n) for n in ticks[0]])
else:
x_formatter = FuncFormatter(lambda n, pos: '$%s$' % latex(n))
elif isinstance(x_formatter, (list, tuple)):
if (not isinstance(ticks[0], (list, tuple)) or
len(ticks[0]) != len(x_formatter)):
Expand All @@ -2444,7 +2448,11 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
LogFormatterMathtext(base=base[1])(n, pos).replace(
"\\mathdefault", ""))
else:
y_formatter = FuncFormatter(lambda n, pos: '$%s$' % latex(n))
# circumvent the problem of symbolic tick values (trac #34693)
if isinstance(y_locator, FixedLocator):
y_formatter = FixedFormatter(['$%s$' % latex(n) for n in ticks[1]])
else:
y_formatter = FuncFormatter(lambda n, pos: '$%s$' % latex(n))
elif isinstance(y_formatter, (list, tuple)):
if (not isinstance(ticks[1], (list, tuple)) or
len(ticks[1]) != len(y_formatter)):
Expand Down
6 changes: 3 additions & 3 deletions src/sage/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,15 +1741,15 @@ def b(n): return lambda x: bessel_J(n, x) + 0.5*(n-1)
::
sage: plot(2*x+1,(x,0,5),ticks=[[0,1,e,pi,sqrt(20)],2],tick_formatter="latex") # not tested (broken with matplotlib 3.6)
sage: plot(2*x + 1,(x, 0, 5), ticks=[[0, 1, e, pi, sqrt(20)], [1, 3, 2*e + 1, 2*pi + 1, 2*sqrt(20) + 1]], tick_formatter="latex")
Graphics object consisting of 1 graphics primitive
.. PLOT::
g = plot(2*x+1,(x,0,5),ticks=[[0,1,e,pi,sqrt(20)],2],tick_formatter="latex")
g = plot(2*x + 1,(x, 0, 5), ticks=[[0, 1, e, pi, sqrt(20)], [1, 3, 2*e + 1, 2*pi + 1, 2*sqrt(20) + 1]], tick_formatter="latex")
sphinx_plot(g)
This is particularly useful when setting custom ticks in multiples of `pi`.
This is particularly useful when setting custom ticks in multiples of `\pi`.
::
Expand Down

0 comments on commit 949866d

Please sign in to comment.