Skip to content

Instantly share code, notes, and snippets.

View demisjohn's full-sized avatar

Demis D. John demisjohn

  • Univ. of California Santa Barbara
  • Santa Barbara, CA, USA
  • 10:43 (UTC -08:00)
View GitHub Profile
@demisjohn
demisjohn / Pickle: Save+Load MPL Figures to file.py
Last active February 11, 2021 15:14
How to use Python's `pickle` to save/load matplotlib Figures to/from a file.
'''
To save python objects of any sort, to a file.
'''
import pickle as pkl
pkl.dump( fig, open('FigureObject.pickle', 'wb') )
pkl.dump( fig, open('FigureObject.pickle', 'wb'), fix_imports=True ) # fix_imports makes it py2x compatible - untested
'''
Load python objects from file
@demisjohn
demisjohn / Python RegEx Notes.py
Last active January 20, 2016 19:17
Notes on using RegEx to parse strings in Python
import re # regex
# This will be used to capture a group () within a larger string, and save that group to a variable
# Set regex pattern to match
dcpattern = re.compile( r'DC=[-]?(\d*\.?\d*)V?' , flags=(re.IGNORECASE) )
#regex expression within 'raw' python string, to prevent interpretation/escaping (%f etc.).
# The Regular Expression
# DC=[-]?(\d*\.?\d*)V?
@demisjohn
demisjohn / Plot ThermalImage v2 GUI LineTrace.py
Created January 20, 2016 15:26
Click-Drag to plot lineSlice of a 2D plot
import numpy as np
import matplotlib.pyplot as plt
# Handle mouse clicks on the plot:
class LineSlice:
'''Allow user to drag a line on a pcolor/pcolormesh plot, and plot the Z values from that line on a separate axis.
Example
-------
fig, (ax1, ax2) = plt.subplots( nrows=2 ) # one figure, two axes