0

I am using the plot_implicit() function from sympy, and when I try and access the matplotlib backend like everyone says to do, I get the following error: Plot object has no attribute '_backend'

I have tried 'import spb' after doing 'pip install sympy_plot_backends'. I have also tried declaring explicitly p = sympy.plot_implicit(...) as compared to p = plot_implicit(..) which expectedly changed nothing.

import sympy

p = sympy.plot_implicit(formattedQ, show=False)
fig = p._backend.fig
fig.set_title("asdfj")

Traceback

stderr:     fig = p._backend.figAttributeError: 'Plot' object has no    attribute '_backend'

1 Answers1

0

In the current (1.18) sympy version, the old way to access the matplotlib isn't supported anymore.

Sympy's plotting lists a number of elements you can change or add to a sympy plot. The list is quite long, but unfortunately not documented in detail. Examples are title, label, xlabel, xscale, xlim, ... You can also add markers, rectangles and annotations via lists of dictionaries. See e.g. How to customize and add extra elements to Sympy plots

To add a title, you can just write sympy.plot_implicit(..., title='my title').

If you need more matplotlib fine-tuning, you can "move the plot to matplotlib", as described in Display two Sympy plots as two Matplotlib subplots.

For your example, the code could look like:

import matplotlib.pyplot as plt
from sympy import plot_implicit, Eq, S
from sympy.abc import x, y

def move_sympyplot_to_axes(p, ax):
    backend = p.backend(p)
    backend.ax = ax
    backend._process_series(backend.parent._series, ax, backend.parent)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_position('zero')
    ax.set_xlabel(ax.get_xlabel(), ha='right', x=1, labelpad=0)
    ax.set_ylabel(ax.get_ylabel(), ha='right', va='top', y=1, labelpad=0, rotation=0)
    plt.close(backend.fig)

formattedQ = Eq (x**(S(2)/3) + y**(S(2)/3), 8)
p = plot_implicit(formattedQ, (x, -25, 25), (y, -25, 25), show=False)

fig, ax = plt.subplots()
move_sympyplot_to_axes(p, ax)
ax.set_title('formattedQ')
ax.set_aspect('equal')

plt.show()

move sympy plot to matplotlib

JohanC
  • 71,591
  • 8
  • 33
  • 66