1

I am using seaborn for lineplot. I want to store all the plot objects in dict and show (display) the plot later accoding to the dict key.

lookup = df_to_plot['lookup'].unique()
figures = {}
for key in lookup:
      figures[key] = plt.figure()
      datadf = df_to_plot[df_to_plot['lookup'] == key]
      sns.lineplot(data=datadf, x ='year', y='value', hue='tech_low')

Later I want to show the plots like:

key = 'xyz'
plt = figures[key]
plt.show()

Any suggestion how to achieve this?

1 Answers1

0

It's a bit hacky it uses the internal _pylab_helpers module: the following example silently creates 3 Figures, stores them in the dict and then brings up the second Figure:

import matplotlib.pyplot as plt
from matplotlib import _pylab_helpers

figures = {}
for i in range(3):
    fig, ax = plt.subplots()
    fig.suptitle(f"Figure {i}")
    figures[f"Fig{i}"] = fig
    plt.close()


def show_fig(fig):
    _pylab_helpers.Gcf.set_active(fig.canvas.manager)
    plt.show()


show_fig(figures["Fig1"])  
Stef
  • 28,728
  • 2
  • 24
  • 52