How do I change the color of the shaded region for the plot_implicit function from the sympy_plot_backends module?

Question:

I cannot set the color of the shaded region while using plot_Implicit from the spb module. It should be as simple as passing a ‘colors’ parameter as that’s what the backend contourf function takes, but it doesn’t work.

I have tried plot = plot_implicit(formattedQ, (x,-5,5),(y,-5,5),show=False,adaptive =False, rendering_kw={'colors': 'red'})` as well as plot = plot_implicit(formattedQ, (x,-5,5),(y,-5,5),show=False,adaptive =False, rendering_kw={'colors': ['red']}) and plot = plot_implicit(formattedQ, (x,-5,5),(y,-5,5), show=False, adaptive=False, line_color='red'), as well as a bunch of similar variations. All I am trying to do is change the color of the lines plotted, NOT by using cmap. I either end up getting an error like this

stderr: raise ValueError(‘Either colors or cmap must be None’)`

or no error but the graph remains unchanged.

Minimal reproducible example:

from spb import plot_implicit
from sympy import symbols

x, y = symbols("x y")
plot = plot_implicit(
    eval("y<x"),
    (x, -5, 5),
    (y, -5, 5),
    show=True,
    adaptive=False,
    rendering_kw={"colors": ["red"]},
)
Asked By: Joshua Guevara

||

Answers:

plot_implicit uses two strategies to visualize expressions:

  • if adaptive=True, it uses matplotlib’s fill command. As of version 1.6.7, it is not possible to set custom rendering keywords for this kind of plots.
  • if adaptive=False, it uses matplotlib’s contour or contourf, which requires colormaps. This is how you can set your custom colormaps:
from matplotlib.colors import ListedColormap

# NOTE: #ffffff00 is transparent color
cmap = ListedColormap(["#ffffff00", "red"])

x, y = symbols("x y")
plot = plot_implicit(
    eval("y<x"),
    (x, -5, 5),
    (y, -5, 5),
    show=True,
    adaptive=False,
    rendering_kw={"cmap": cmap},
)
Answered By: Davide_sd
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.