0

I have some figures and axes saved to pickle files using:

pickle.dump((fig, ax), open('myplot.pickle', 'wb'))

I'm trying to load this later in a separate file, apply a new style file to it, and export it again:

plt.style.use("some_fancy_style")

fig, ax = pickle.load(open("myplot.pickle", "rb"))

fig.savefig("myplot_different_style.png")

But this only picks up a few settings related to savefig from the style file. Is there a (relatively easy) way to recreate the plot so that axes/font/grid/etc config are also picked up?

I know even to show the figure after loading the pickle we have to create a new dummy figure and hijack its canvas manager (source):

new_manager = plt.figure().canvas.manager
new_manager.canvas.figure = fig
fig.set_canvas(new_manager.canvas)

I was wondering if similar things could be done for axes/grid/etc?

Milad
  • 4,901
  • 5
  • 32
  • 43
  • I guess this is essentially what this repo from NASA tries to achieve: https://github.com/nasa/mplStyle It seems to be out of date so couldn't really get it to work – Milad May 30 '22 at 11:05
  • pickle saves a pretty dead version of the figure. You are _far_ better to save the data and the script for plotting and recreate that way. – Jody Klymak May 31 '22 at 13:36

0 Answers0