Skip to content

Instantly share code, notes, and snippets.

@andrewfowlie
Created December 2, 2016 07:40
Show Gist options
  • Save andrewfowlie/2081657f8289bb82a0e121625affb917 to your computer and use it in GitHub Desktop.
Save andrewfowlie/2081657f8289bb82a0e121625affb917 to your computer and use it in GitHub Desktop.
Overlay two pickled figure objects
"""
Merge two pickled plots
=======================
This is rather crude. The pickled plots should have identical sizes, axes etc.
"""
from pickle import load
from matplotlib.pyplot import figure
from argparse import ArgumentParser as arg_parser
def pickles_to_plot(pickle_names, combined_name="combined.pdf", alpha=0.3):
"""
Combine pickled figures into a single figure.
:param pickle_names: Name of pickled figures
:type pickle_names: list of str
:param combined_name: Name of resulting figure saved to disk
:type combined_name: str
:param alpha: Transparency of overlaid figures
:type alpha: float
"""
fig_handles = [load(open(f, 'rb')) for f in pickle_names]
ax_handles = [f.gca() for f in fig_handles]
figsize = fig_handles[0].get_size_inches()
fig = figure(figsize=figsize)
for a in ax_handles:
a.patch.set_alpha(alpha)
fig._axstack.add(fig._make_key(a), a)
fig.tight_layout()
fig.savefig(combined_name)
if __name__ == "__main__":
parser = arg_parser(description="Merge pickled plots into a single figure.")
parser.add_argument('pickle_names', nargs='+', type=str, help="Pickled figures")
parser.add_argument('-n', '--combined_name', type=str, required=False, default='combined.pdf', help="Name of combined figure to save to disk")
parser.add_argument('-a', '--alpha', type=float, required=False, default=0.3, help="Transparency of overlaid figures")
args = parser.parse_args()
pickles_to_plot(args.pickle_names, args.combined_name, args.alpha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment