Skip to content
Advertisement

Python SymPy’s plotting legend when using multiple ranges

I am using SymPy’s plotting module, but legends stop working when I start having multiple lines with different ranges (to plot a function f(x) whose definition depend on x like in the following example). I actually haven’t seen any example of this anywhere :

JavaScript

Here the legend is ignored. I also tried

JavaScript

instead of specifying plt[0] but Python says

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

My final goal would be something like a plot with lines of multiple colors, representing functions which have a different definition before and after a given value of x and I append them together, say, if plt1 and plt2 have both 2 parts,

JavaScript

Does anyone know how label and legend work in this context? Thank you.

Advertisement

Answer

The simplest thing is to use matplotlib directly. You can ask sympy for the list of points that it would plot and then use them with matplotlib yourself like this:

JavaScript

Then you can use matplotlib’s legend function.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement