How to use MatplotlibBackend?

115 views
Skip to first unread message

Matthias Geier

unread,
Nov 9, 2024, 3:24:14 PM11/9/24
to sy...@googlegroups.com
Hi all.

A few years ago, I have documented how to create Matplotlib animations
from SymPy plots:
https://nbviewer.org/github/mgeier/python-audio/blob/master/sympy/sympy-matplotlib-animations.ipynb

This has worked quite well (as can be seen in the link), but in the
meantime, the behavior/usage of the MatplotlibBackend seems to have
changed and some of the approaches don't work anymore with the latest
SymPy/Matplotlib versions.

Most notably, the process_series() method doesn't seem to have the
desired effect anymore.

Note that the approach using get_points() still works perfectly, and
the approach using get_segments() is deprecated, and is not expected
to work anymore.

Does anyone have an idea how to make the approaches using
MatplotlibBackend work again?

cheers,
Matthias

Aaron Meurer

unread,
Nov 11, 2024, 2:38:52 PM11/11/24
to sy...@googlegroups.com
I haven't followed all the changes to plotting. Do you know what the
PR SymPy was that changed this?

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/sympy/CAFesC-dx1U1%2BLHDpU4J-SqLs21cszhCaDXSLYSf2dEuTDEjYDg%40mail.gmail.com.

Richard Katz

unread,
Nov 11, 2024, 4:44:44 PM11/11/24
to sy...@googlegroups.com
Hi Aaron,


Have you tried calling it from Python itself? I tried looking up "call matplotlib from jupyter and it just pointed to this:


import matplotlib.pyplot as plt

To use Matplotlib in a Jupyter Notebook, follow these steps:
1. Import Matplotlib:
Python
import matplotlib.pyplot as plt


HTH.,

-- Rich

 

Richard Katz

Data Engineering Architect   

San Leandro, CA: San Francisco Bay Area:
https://www.linkedin.com/in/richardkatz/

415 336-9559





--

Matthias Geier

unread,
Nov 12, 2024, 1:38:32 PM11/12/24
to sy...@googlegroups.com
Hi Aaron.

On Mon, Nov 11, 2024 at 8:38 PM Aaron Meurer wrote:
>
> I haven't followed all the changes to plotting. Do you know what the
> PR SymPy was that changed this?

That's not so straightforward ... different things seem to have broken
at different times. But maybe I wasn't using them correctly in the
first place?

The example which assigns to "parent"
(https://nbviewer.org/github/mgeier/python-audio/blob/master/sympy/sympy-matplotlib-animations.ipynb#Changing-the-Whole-Plot)
still partially works with
https://github.com/sympy/sympy/commit/5d1e056.

I'm saying "partially", because it now cycles the plotting colors,
which it didn't do before (see below for the cause of that problem).

One commit later (https://github.com/sympy/sympy/commit/46558492a7),
I'm getting this exception:

AttributeError: 'MatplotlibBackend' object has no attribute 'fig'

The "fig" attribute is now not set in the constructor anymore, but
instead at a later point, e.g. when calling process_series(). So
should I call that first?

If yes, it is still not working. Even though this avoids the
exception, now the animation simply consists of an empty plot (i.e.
only the axes are visible).

BTW, I'm using "show=False". When I drop that, the "has no attribute
'fig'" goes away, but the animation still doesn't work (instead, there
are a few stray plots generated).

Going back in the history, I found that
https://github.com/sympy/sympy/commit/79a2cd5432177f662b2bebe1ffc3b3bc4ca575b9
is the commit that introduces the color cycling.
According to the commit message, the color change is definitely
intentional, so I guess I'll have to try to work around that behavior
change.

The first example still doesn't work at this point in the history. It
just shows an empty plot (only the axes are visible).

I wanted to go further back in history, but I wasn't able to install a
combination of SymPy/NumPy/Matplotlib/Python that worked.

Is there anything else I should try?

cheers,
Matthias
> To view this discussion visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6K1hVkWmw3FZ0nHBemS4oBkAqC4kRHdR9uxSXXk6f21EA%40mail.gmail.com.

Aaron Meurer

unread,
Nov 12, 2024, 3:32:25 PM11/12/24
to sy...@googlegroups.com
On Tue, Nov 12, 2024 at 11:38 AM Matthias Geier
<matthia...@gmail.com> wrote:
>
> Hi Aaron.
>
> On Mon, Nov 11, 2024 at 8:38 PM Aaron Meurer wrote:
> >
> > I haven't followed all the changes to plotting. Do you know what the
> > PR SymPy was that changed this?
>
> That's not so straightforward ... different things seem to have broken
> at different times. But maybe I wasn't using them correctly in the
> first place?
>
> The example which assigns to "parent"
> (https://nbviewer.org/github/mgeier/python-audio/blob/master/sympy/sympy-matplotlib-animations.ipynb#Changing-the-Whole-Plot)
> still partially works with
> https://github.com/sympy/sympy/commit/5d1e056.
>
> I'm saying "partially", because it now cycles the plotting colors,
> which it didn't do before (see below for the cause of that problem).
>
> One commit later (https://github.com/sympy/sympy/commit/46558492a7),
> I'm getting this exception:
>
> AttributeError: 'MatplotlibBackend' object has no attribute 'fig'

It seems like that commit was supposed to just be a refactor, so if it
broke some use-case, it was likely unintentional.

It's not clear to me whether the things you're doing are supposed to
be supported or not (again, I don't really know much about the
plotting module). I would say that animation is something that should
be doable, either by being directly supported or by being
straightforward to hook into what SymPy gives you.

Aaron Meurer
> To view this discussion visit https://groups.google.com/d/msgid/sympy/CAFesC-cvTU-VOsMgoCRAdWE5jzrSVG5%3Dk-dASH%2BXwaXo8HJ63w%40mail.gmail.com.

Matthias Geier

unread,
Nov 21, 2024, 4:02:58 PM11/21/24
to sy...@googlegroups.com
Hi all.

Just a little update: I got a few answers from Davide (who did the
recent plotting refactors) at
https://github.com/mgeier/python-audio/issues/4.

If anyone has further advice, please reply here or there.

cheers,
Matthias
> To view this discussion visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KC6QCJt0Y%3DTsa1rFSTRbeMWCgN-WxMxaqx91xQdwb%2BQw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages