seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

Consider Plot.tweak method for accepting function that operates on matplotlib figure

Open mwaskom opened this issue 2 years ago • 0 comments

In some cases, it will be easy for users to achieve some fussy customization by writing imperative matplotlib code rather than trying to fit it into the declarative seaborn spec.

We could/should make it possible to access the matplotlib figure / axes, so the plot could be compiled and then tweaked. Currently, Plot._figure is private, although you can use Plot.on to have a reference to the relevant matplotlib object, like

Plot(...).add(...).on(ax:=plt.gca()).plot()
ax.set(...)

But both patterns are still a little cumbersome as you need to (1) trigger the plot to compile by calling .plot() (2) catch the return value, fiddle with the matplotlib objects, and (3) show/display the plot.

An alternative pattern would be to have Plot.tweak, a method that accepts a callable function, where that function should consume a matplotlib figure and operate on it. It would be used toward the end of the plotting pipeline. (Although perhaps that should be flexible? e.g. allow .tweak(f, before=True) to do some custom setup?)

One thing to consider is that having the passed function consume a figure is the most general approach, but you're typically going to want to operate on the axes. Should that be allowed and if so, how? (e.g. .tweak(f, axes=True)?

mwaskom avatar Jul 30 '22 22:07 mwaskom