Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

In [9]: import sympy                                                                                                                           

In [10]: line1, line2 = sympy.plot((x**2,(x,-1,0)),(x**3,(x,0,1)),label='$f(x)$',show=False)                                                   

In [11]: x1, y1 = line1.get_points()                                                                                                           

In [12]: import matplotlib.pyplot as plt                                                                                                       

In [13]: plt.plot(x1, y1)                                                                                                                      
Out[13]: [<matplotlib.lines.Line2D at 0x120a9da90>]

In [14]: plt.show()  

Then you can use matplotlib's legend function.

Post a Comment for "Python Sympy's Plotting Legend When Using Multiple Ranges"