2025, Oct 28 19:00

Fixing SymPy plot_implicit errors for Boolean inequalities with Abs using SymPy Plotting Backends

Learn why SymPy plot_implicit fails on Boolean inequalities with Abs and how SymPy Plotting Backends fixes it by switching from adaptive to uniform meshing.

Plotting boolean implicit regions with SymPy: why it fails and how to fix it

Combining inequalities into a single implicit region often looks straightforward in SymPy—until it suddenly isn’t. A typical case is plotting an inequality that mixes a Boolean composition with Abs. The snippet below runs fine for individual conditions, yet the combined plot raises an exception when using the standard plot_implicit.

The failing example

from sympy import *
u, v = symbols('u v')
rule_a = v + 2*Abs(u) > 0
rule_b = u > 0
rule_c = v < 0
fig_a = plot_implicit(rule_a)
fig_b = plot_implicit(rule_b)
fig_c = plot_implicit(And(rule_b, rule_c))
try:
    fig_d = plot_implicit(And(rule_a, rule_b))
except:
    print("Why is this not working")

The last call is the problematic one: plotting And(rule_a, rule_b) throws, while the earlier individual plots are fine.

What actually happens

Using SymPy Plotting Backends (spb) resolves the issue. The developer of SymPy Plotting Backends fixed the bug and published a new release with the correction. The package is already available on PyPI and is expected on Conda shortly. With the updated spb, the same boolean expression that mixes And with Abs can be plotted successfully.

There is a bit of algorithmic detail worth knowing. In spb, plot_implicit can evaluate expressions in two ways. It normally samples a uniform grid of points. When it receives a Boolean expression (for example with And or Or), it automatically switches to an adaptive algorithm based on interval arithmetic because that usually yields better results. However, an expression containing Abs cannot be handled by the adaptive routine. In that situation, the code detects the limitation and falls back to uniform sampling. When you run it, you will see warnings that explain the switch in strategy:

UserWarning: The provided expression contains Boolean functions. In order to plot the expression, the algorithm automatically switched to an adaptive sampling.

UserWarning: Adaptive meshing could not be applied to the expression. Using uniform meshing.

A practical fix with SymPy Plotting Backends

Here is a working setup using spb. The ordering of symbols defines the axes, and a couple of small tweaks make the output cleaner. If no ranges are specified, plot_implicit limits both axes to [-10, 10]. Choosing an odd n puts sample points exactly on u=0 and v=0, which improves the appearance of boundaries. For the combined boolean region, increasing n further produces a smoother result.

from sympy import *
from spb import *
var("u v")
rule_a = v + 2*Abs(u) > 0
rule_b = u > 0
rule_c = v < 0
combo = And(rule_a, rule_b)
fig1 = plot_implicit(rule_a, u, v, n=101, grid=False, title=str(rule_a))
fig2 = plot_implicit(rule_b, u, v, grid=False, title=str(rule_b))
fig3 = plot_implicit(combo, u, v, n=1001, grid=False, title=str(combo))

Run this with the updated spb and you should get the expected plots. The warnings are informational and reflect the automatic switch from adaptive to uniform meshing due to Abs.

Why this matters

Implicit plotting of logical combinations of inequalities is a common workflow for visualizing feasible regions and constraints. Without robust handling of Boolean expressions, especially those involving Abs, you may run into exceptions or misleading plots. Knowing that spb handles these cases by attempting an adaptive pass and then cleanly falling back to uniform sampling means you can rely on consistent output, even for tricky expressions.

Conclusion and practical advice

If a boolean implicit plot fails under the standard approach, try SymPy Plotting Backends’ plot_implicit. Keep an eye on the console: the warnings tell you when the algorithm switches modes. Specify the variable order explicitly so the axes are unambiguous, and use an odd n to place samples on the axes; for more complex combined regions, increase n to improve visual quality. The updated package with the fix is available on PyPI and is expected on Conda in the near term. If you run into other issues, report them at the project’s repository: https://github.com/Davide-sd/sympy-plot-backends/issues. For usage details, see the docs: https://sympy-plot-backends.readthedocs.io.

The article is based on a question from StackOverflow by Franz and an answer by Davide_sd.