[Matplotlib-users] Declarative interface to matplotlib

Hi everyone,

I’d like to ask you a general question, as someone who isn’t very experienced with matplotlib.

I noticed that matplotlib is very imperative, in contrast to declarative. For example, I wanted to show an imshow plot with a colorbar, so I needed to first create the plot, then run plt.colorbar(), then show the plot. In more complicated plots, you could be doing 10+ actions one after another to create a plot.

I would find a declarative interface to be much more elegant, i.e. one call to create the plot and lots of keyword arguments, some of them nested, to specify all the options. Of course, matplotlib being a very mature project, the current interface must be maintained forever. But, do you know of any such declarative interface to matplotlib? Either as part of the library, or a separate library that uses matplotlib?

If such a thing doesn’t exist, what would you think about adding one?

Thanks,
Ram.

Hi,

The MetPy project has worked on a declarative, matplotlib-based plotting interface:

https://unidata.github.io/MetPy/latest/examples/index.html#plotting
https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.html
https://unidata.github.io/python-training/gallery/declarative_500_hpa/

This focuses, though, on domain-specific applications in meteorology and atmospheric science, leveraging the traitlets library. The interface was also designed to facilitate transition away from a legacy, declarative-like plotting tool.

Not claiming by any means we have the interface perfect. We spent a lot of time trying to come up with new names for Figure and Axes in this interface since we did not want to collide with matplotlib’s own classes. Honestly, the basic interface is pretty trivial to create (at least using traitlets). The challenge is to handle state update and invalidation properly when trying to support interactive use cases (especially when all of the classes are thin wrappers around matplotlib state).

Ryan

···

Ryan May

Thanks Ryan, that’s interesting.

···

On Sun, Aug 23, 2020 at 10:31 PM Ryan May <rmay31@gmail.com> wrote:

Hi,

The MetPy project has worked on a declarative, matplotlib-based plotting interface:

https://unidata.github.io/MetPy/latest/examples/index.html#plotting
https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.html
https://unidata.github.io/python-training/gallery/declarative_500_hpa/

This focuses, though, on domain-specific applications in meteorology and atmospheric science, leveraging the traitlets library. The interface was also designed to facilitate transition away from a legacy, declarative-like plotting tool.

Not claiming by any means we have the interface perfect. We spent a lot of time trying to come up with new names for Figure and Axes in this interface since we did not want to collide with matplotlib’s own classes. Honestly, the basic interface is pretty trivial to create (at least using traitlets). The challenge is to handle state update and invalidation properly when trying to support interactive use cases (especially when all of the classes are thin wrappers around matplotlib state).

Ryan

On Sun, Aug 23, 2020 at 11:39 AM Ram Rachum <ram@rachum.com> wrote:

Hi everyone,

I’d like to ask you a general question, as someone who isn’t very experienced with matplotlib.

I noticed that matplotlib is very imperative, in contrast to declarative. For example, I wanted to show an imshow plot with a colorbar, so I needed to first create the plot, then run plt.colorbar(), then show the plot. In more complicated plots, you could be doing 10+ actions one after another to create a plot.

I would find a declarative interface to be much more elegant, i.e. one call to create the plot and lots of keyword arguments, some of them nested, to specify all the options. Of course, matplotlib being a very mature project, the current interface must be maintained forever. But, do you know of any such declarative interface to matplotlib? Either as part of the library, or a separate library that uses matplotlib?

If such a thing doesn’t exist, what would you think about adding one?

Thanks,
Ram.


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Ryan May

I think declarative APIs can be very terse if they are well tuned for a given domain, however as you expand them to cover the general case it is not clear to me that you will be able to preserve that terseness.

Put another way, there is some inherent complexity in making a plot. An imperative API can rely on the host language (Python) to handle some of that complexity whereas a declarative API must absorb all of the complexity. Either it will grow to be as complex as a programming language or it will have to restrict its users to the vocabulary that the original developers imagined. On the other hand, if the declarative API matches what you need (it makes all the right assumptions for your task and data) it can be great!

If you are finding yourself doing the same 10 things every time, I suggest putting that into a function and eventually growing that into a library / declarative API that is tuned to your data / domain / use.

Tom

···

Thomas Caswell
tcaswell@gmail.com

If you want an out of the box decelerative API, plotnine is a ggplot API inspired wrapper over Matplotlib. https://plotnine.readthedocs.io/en/stable/

It also kind of stalled out, but their was some work on an mpl-altair backend to Altair so that you could use the Vegalite grammar and we’d support that project getting picked up again https://github.com/matplotlib/mpl-altair

Like Tom said though, it may be cleaner to start w/ your usecase and build a declarative wrapper around that.

···

On Sun, Aug 23, 2020, 1:39 PM Ram Rachum <ram@rachum.com> wrote:

Hi everyone,

I’d like to ask you a general question, as someone who isn’t very experienced with matplotlib.

I noticed that matplotlib is very imperative, in contrast to declarative. For example, I wanted to show an imshow plot with a colorbar, so I needed to first create the plot, then run plt.colorbar(), then show the plot. In more complicated plots, you could be doing 10+ actions one after another to create a plot.

I would find a declarative interface to be much more elegant, i.e. one call to create the plot and lots of keyword arguments, some of them nested, to specify all the options. Of course, matplotlib being a very mature project, the current interface must be maintained forever. But, do you know of any such declarative interface to matplotlib? Either as part of the library, or a separate library that uses matplotlib?

If such a thing doesn’t exist, what would you think about adding one?

Thanks,
Ram.


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Isn’t Altair trying to be a declarative interface to matplotlib?

Also, I could have sworn someone made a ggplot clone using matplotlib as a base?

Ben Root

···

On Sun, Aug 23, 2020 at 9:30 PM Thomas Caswell <tcaswell@gmail.com> wrote:

I think declarative APIs can be very terse if they are well tuned for a given domain, however as you expand them to cover the general case it is not clear to me that you will be able to preserve that terseness.

Put another way, there is some inherent complexity in making a plot. An imperative API can rely on the host language (Python) to handle some of that complexity whereas a declarative API must absorb all of the complexity. Either it will grow to be as complex as a programming language or it will have to restrict its users to the vocabulary that the original developers imagined. On the other hand, if the declarative API matches what you need (it makes all the right assumptions for your task and data) it can be great!

If you are finding yourself doing the same 10 things every time, I suggest putting that into a function and eventually growing that into a library / declarative API that is tuned to your data / domain / use.

Tom

On Sun, Aug 23, 2020 at 3:59 PM Ram Rachum <ram@rachum.com> wrote:

Thanks Ryan, that’s interesting.

On Sun, Aug 23, 2020 at 10:31 PM Ryan May <rmay31@gmail.com> wrote:

Hi,

The MetPy project has worked on a declarative, matplotlib-based plotting interface:

https://unidata.github.io/MetPy/latest/examples/index.html#plotting
https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.html
https://unidata.github.io/python-training/gallery/declarative_500_hpa/

This focuses, though, on domain-specific applications in meteorology and atmospheric science, leveraging the traitlets library. The interface was also designed to facilitate transition away from a legacy, declarative-like plotting tool.

Not claiming by any means we have the interface perfect. We spent a lot of time trying to come up with new names for Figure and Axes in this interface since we did not want to collide with matplotlib’s own classes. Honestly, the basic interface is pretty trivial to create (at least using traitlets). The challenge is to handle state update and invalidation properly when trying to support interactive use cases (especially when all of the classes are thin wrappers around matplotlib state).

Ryan

On Sun, Aug 23, 2020 at 11:39 AM Ram Rachum <ram@rachum.com> wrote:

Hi everyone,

I’d like to ask you a general question, as someone who isn’t very experienced with matplotlib.

I noticed that matplotlib is very imperative, in contrast to declarative. For example, I wanted to show an imshow plot with a colorbar, so I needed to first create the plot, then run plt.colorbar(), then show the plot. In more complicated plots, you could be doing 10+ actions one after another to create a plot.

I would find a declarative interface to be much more elegant, i.e. one call to create the plot and lots of keyword arguments, some of them nested, to specify all the options. Of course, matplotlib being a very mature project, the current interface must be maintained forever. But, do you know of any such declarative interface to matplotlib? Either as part of the library, or a separate library that uses matplotlib?

If such a thing doesn’t exist, what would you think about adding one?

Thanks,
Ram.


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Ryan May


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Thomas Caswell
tcaswell@gmail.com


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

jinx!

···

On Mon, Aug 24, 2020 at 12:14 AM Benjamin Root <ben.v.root@gmail.com> wrote:

Isn’t Altair trying to be a declarative interface to matplotlib?

Also, I could have sworn someone made a ggplot clone using matplotlib as a base?

Ben Root

On Sun, Aug 23, 2020 at 9:30 PM Thomas Caswell <tcaswell@gmail.com> wrote:

I think declarative APIs can be very terse if they are well tuned for a given domain, however as you expand them to cover the general case it is not clear to me that you will be able to preserve that terseness.

Put another way, there is some inherent complexity in making a plot. An imperative API can rely on the host language (Python) to handle some of that complexity whereas a declarative API must absorb all of the complexity. Either it will grow to be as complex as a programming language or it will have to restrict its users to the vocabulary that the original developers imagined. On the other hand, if the declarative API matches what you need (it makes all the right assumptions for your task and data) it can be great!

If you are finding yourself doing the same 10 things every time, I suggest putting that into a function and eventually growing that into a library / declarative API that is tuned to your data / domain / use.

Tom

On Sun, Aug 23, 2020 at 3:59 PM Ram Rachum <ram@rachum.com> wrote:

Thanks Ryan, that’s interesting.

On Sun, Aug 23, 2020 at 10:31 PM Ryan May <rmay31@gmail.com> wrote:

Hi,

The MetPy project has worked on a declarative, matplotlib-based plotting interface:

https://unidata.github.io/MetPy/latest/examples/index.html#plotting
https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.html
https://unidata.github.io/python-training/gallery/declarative_500_hpa/

This focuses, though, on domain-specific applications in meteorology and atmospheric science, leveraging the traitlets library. The interface was also designed to facilitate transition away from a legacy, declarative-like plotting tool.

Not claiming by any means we have the interface perfect. We spent a lot of time trying to come up with new names for Figure and Axes in this interface since we did not want to collide with matplotlib’s own classes. Honestly, the basic interface is pretty trivial to create (at least using traitlets). The challenge is to handle state update and invalidation properly when trying to support interactive use cases (especially when all of the classes are thin wrappers around matplotlib state).

Ryan

On Sun, Aug 23, 2020 at 11:39 AM Ram Rachum <ram@rachum.com> wrote:

Hi everyone,

I’d like to ask you a general question, as someone who isn’t very experienced with matplotlib.

I noticed that matplotlib is very imperative, in contrast to declarative. For example, I wanted to show an imshow plot with a colorbar, so I needed to first create the plot, then run plt.colorbar(), then show the plot. In more complicated plots, you could be doing 10+ actions one after another to create a plot.

I would find a declarative interface to be much more elegant, i.e. one call to create the plot and lots of keyword arguments, some of them nested, to specify all the options. Of course, matplotlib being a very mature project, the current interface must be maintained forever. But, do you know of any such declarative interface to matplotlib? Either as part of the library, or a separate library that uses matplotlib?

If such a thing doesn’t exist, what would you think about adding one?

Thanks,
Ram.


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Ryan May


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Thomas Caswell
tcaswell@gmail.com


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Thanks for the answers guys!

···

On Mon, Aug 24, 2020 at 7:16 AM Benjamin Root <ben.v.root@gmail.com> wrote:

jinx!

On Mon, Aug 24, 2020 at 12:14 AM Benjamin Root <ben.v.root@gmail.com> wrote:

Isn’t Altair trying to be a declarative interface to matplotlib?

Also, I could have sworn someone made a ggplot clone using matplotlib as a base?

Ben Root

On Sun, Aug 23, 2020 at 9:30 PM Thomas Caswell <tcaswell@gmail.com> wrote:

I think declarative APIs can be very terse if they are well tuned for a given domain, however as you expand them to cover the general case it is not clear to me that you will be able to preserve that terseness.

Put another way, there is some inherent complexity in making a plot. An imperative API can rely on the host language (Python) to handle some of that complexity whereas a declarative API must absorb all of the complexity. Either it will grow to be as complex as a programming language or it will have to restrict its users to the vocabulary that the original developers imagined. On the other hand, if the declarative API matches what you need (it makes all the right assumptions for your task and data) it can be great!

If you are finding yourself doing the same 10 things every time, I suggest putting that into a function and eventually growing that into a library / declarative API that is tuned to your data / domain / use.

Tom

On Sun, Aug 23, 2020 at 3:59 PM Ram Rachum <ram@rachum.com> wrote:

Thanks Ryan, that’s interesting.

On Sun, Aug 23, 2020 at 10:31 PM Ryan May <rmay31@gmail.com> wrote:

Hi,

The MetPy project has worked on a declarative, matplotlib-based plotting interface:

https://unidata.github.io/MetPy/latest/examples/index.html#plotting
https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.html
https://unidata.github.io/python-training/gallery/declarative_500_hpa/

This focuses, though, on domain-specific applications in meteorology and atmospheric science, leveraging the traitlets library. The interface was also designed to facilitate transition away from a legacy, declarative-like plotting tool.

Not claiming by any means we have the interface perfect. We spent a lot of time trying to come up with new names for Figure and Axes in this interface since we did not want to collide with matplotlib’s own classes. Honestly, the basic interface is pretty trivial to create (at least using traitlets). The challenge is to handle state update and invalidation properly when trying to support interactive use cases (especially when all of the classes are thin wrappers around matplotlib state).

Ryan

On Sun, Aug 23, 2020 at 11:39 AM Ram Rachum <ram@rachum.com> wrote:

Hi everyone,

I’d like to ask you a general question, as someone who isn’t very experienced with matplotlib.

I noticed that matplotlib is very imperative, in contrast to declarative. For example, I wanted to show an imshow plot with a colorbar, so I needed to first create the plot, then run plt.colorbar(), then show the plot. In more complicated plots, you could be doing 10+ actions one after another to create a plot.

I would find a declarative interface to be much more elegant, i.e. one call to create the plot and lots of keyword arguments, some of them nested, to specify all the options. Of course, matplotlib being a very mature project, the current interface must be maintained forever. But, do you know of any such declarative interface to matplotlib? Either as part of the library, or a separate library that uses matplotlib?

If such a thing doesn’t exist, what would you think about adding one?

Thanks,
Ram.


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Ryan May


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Thomas Caswell
tcaswell@gmail.com


Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users