Search code examples
pythonpython-3.xsympy

How to display grid on plot in sympy


I would like to display a grid with a plot using Sympy:

import sympy
from sympy import sin
from sympy.abc import x
from math import pi
sympy.plot(sin(x),xlim=(0,2*pi))

Plot using Sympy

Using matplotlib it is straight forward to add a grid :

import matplotlib.pylab as plt
plt.grid(True)
plt.show()

Grid using matplotlib

How can I do this with Sympy?


Solution

  • Since SymPy uses matplotlib under the hood if you have it available, you can use:

    from matplotlib import style
    style.use('ggplot')
    

    And that will style your sympy.plot()

    To print all available styles, use:

    import matplotlib as plt
    print(plt.style.available)
    

    Here's a link about style for matplotlib