Davide-sd / sympy-plot-backends

An improved plotting module for SymPy
BSD 3-Clause "New" or "Revised" License
42 stars 9 forks source link

Options for horizontal and vertical 0 lines, grid, ticks #24

Closed imakn634 closed 1 year ago

imakn634 commented 1 year ago

"SymPy Plotting Backends"(SPB) is really the great enhancement of the SymPy plot().
It would be nicer if the following matplotlib.pyplot.plot() functions were also available in SPB.

  1. horizontal and vertical 0 lines: The issue closed, but I prefer something like...
    plt.axhline(0, color='black', dashes=(5, 5), linewidth=0.6)
    plt.axvline(0, color='black', dashes=(5, 5), linewidth=0.6)
  2. grid linestyle: something like...
    plt.grid(which="major", color="lightgray", ls="--", linewidth=0.5)
  3. ticks setting, positions and labels: capability to customize something like...
    plt.xticks(
    ticks = np.linspace(-np.pi, np.pi, 5), 
    labels = ["-$\pi$", "-0.5$\pi$", "0", "0.5$\pi$", "$\pi$"]
    )
    plt.yticks(np.arange(-1., 1.1, 0.5))
Davide-sd commented 1 year ago

Hello @imakn634 ,

Everything you asked is already implemented in Matplotlib, there is no need to re-implement it on this module. In fact, this module is meant to be a starting point for plotting: once your symbolic expressions are plotted, you can extract the figure and axes object and apply all the customization you'd like, something like this:

from sympy import *
from spb import *
import matplotlib.ticker as tck
var("x")

p = plot(cos(x), (x, -0, 2*pi), show=False)
fig, ax = p.fig, p.ax
ax.minorticks_on()
ax.grid(True, which="minor", color="lightgray", ls="--", linewidth=0.25)
ax.axhline(-0.1, color='black', dashes=(5, 5), linewidth=1)
ax.axhline(0.1, color='black', dashes=(5, 5), linewidth=1)
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))

image

imakn634 commented 1 year ago

This is very much helpful. Thank you!