Commit 0f14c0dc authored by Tobias Hangleiter's avatar Tobias Hangleiter
Browse files

Add save/load matplotlib figure like in MATLAB

parent ace901bd
import sys
import csv
import os.path
import pickle
def show_saved_figure(path):
"""Unpickle and show a pickled matplotlib figure.
Parameters
----------
path: str | pathlib.Path
The path where the figure is saved.
Returns
-------
fig: matplotlib.figure.Figure
The matplotlib figure instance.
"""
fig = pickle.load(open(path, 'rb'))
import matplotlib.pyplot as plt
dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = fig
fig.set_canvas(new_manager.canvas)
plt.show()
return fig
def save_figure(fig, path):
"""Pickle a matplotlib figure.
Parameters
----------
fig: matplotlib.figure.Figure
The matplotlib figure instance to be saved.
path: str | pathlib.Path
The path where the figure is saved.
"""
pickle.dump(fig, open(path, 'wb'))
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via input() and return their answer.
......@@ -52,11 +94,11 @@ class CsvLogger:
if reader.fieldnames != fieldnames:
raise RuntimeError("Existing file has differing fieldnames", reader.fieldnames, fieldnames)
else:
with open(self.filename, 'x') as file:
csv.DictWriter(file, fieldnames=self.fieldnames, dialect=self.dialect).writeheader()
def write(self, *args):
with open(self.filename, 'a+') as file:
csv.DictWriter(file, fieldnames=self.fieldnames, dialect=self.dialect).writerow(dict(zip(self.fieldnames, args)))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment