mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.62k stars 1.94k forks source link

Consider adding mpl.Figure as an optional argument to subplot functions / classes. #3734

Closed colm-brandon-ul closed 4 months ago

colm-brandon-ul commented 4 months ago

I've recently be working on an interactive visualisation tool and several seaborn fn's generate the mpl figure internally. When doing interactive plots with ipython you ideally have a single figure and redraw it, which is incompatible with a lot of seaborn's plotting fn's that contain subplots (e.g. FacetGrid).

Not sure if this use-case is overly niche or if there's a reason I've overlooked for not faciliating this functionality, but if that isn't the case a solution may be to allow a figure to be passed as an argument (shown below).

class FacetGrid(Grid):

  def __init__(
          self, data, *,
          row=None, col=None, hue=None, col_wrap=None,
          sharex=True, sharey=True, height=3, aspect=1, palette=None,
          row_order=None, col_order=None, hue_order=None, hue_kws=None,
          dropna=False, legend_out=True, despine=True,
          margin_titles=False, xlim=None, ylim=None, subplot_kws=None,
          gridspec_kws=None, fig=None
      ):
    ...
    if fig:
      ...
    else:
      with _disable_autolayout():
        fig = plt.figure(figsize=figsize)
    ...
mwaskom commented 4 months ago

Hi — this has been discussed a few times if you search!