Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Joe Kington joferkington

🏠
Working from home
Block or report user

Report or block joferkington

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View circular_colorbar_demo.py
import numpy as np
from scipy.interpolate import Rbf
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from mpl_toolkits.axes_grid1 import inset_locator
from matplotlib.projections.polar import PolarAxes
np.random.seed(1977)
def main():
x, y, z = generate_surface()
View example_scrolling_plot.py
import Tkinter as tk
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
def main():
root = tk.Tk()
app = Application(root)
tk.mainloop()
View multicursor_multispan_demo.py
import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor
import numpy as np
class SynchedMultiCursor(MultiCursor):
def __init__(self, *args, **kwargs):
self._enabled = True
MultiCursor.__init__(self, *args, **kwargs)
def onmove(self, event):
View scatter_vs_multiple_plot_calls.py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
num = 100000
x, y = np.random.random((2, num))
category = np.random.randint(1, 5, num)
# Using multiple "plot" calls
fig, ax = plt.subplots()
@joferkington
joferkington / slice_explorer.py
Created May 31, 2013
Simple pcolormesh data explorer
View slice_explorer.py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
def main():
data = np.random.random((10,10,10))
ex = Explorer(data)
ex.show()
class Explorer(object):
@joferkington
joferkington / gist:5074650
Created Mar 3, 2013
Print the largest (by area) contiguous object in an array
View gist:5074650
import numpy as np
import scipy.ndimage as ndimage
# The array you gave above
data = np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
@joferkington
joferkington / struct_vs_numpy_example.py
Last active Dec 7, 2015
Writing an arbitrary, pre-existing numpy array to a hetergenous binary format
View struct_vs_numpy_example.py
import numpy as np
# Our input data...
x = np.random.randint(0, 3200, (1000,1000))
# We're replacing something like
# struct.pack(">"+"hB"*x.size)
# Note that that's a 2-byte signed int followed by 1-byte unsigned
# We'll need to create the output 1D array and assign manually:
@joferkington
joferkington / excel_col_names.py
Created Oct 10, 2012
excel-style label conversion
View excel_col_names.py
"""MIT license"""
import xlrd
import re
def col2index(name):
name = name.upper()
col = -1
for i, letter in enumerate(name[::-1]):
col += (ord(letter) - ord('A') + 1) * 26**i
View gist:2465280
import numpy as np
def process_file(filename, num_cols, delimiter='\t'):
def items(infile):
for line in infile:
for item in line.rstrip().split(delimiter):
yield float(item)
with open(filename, 'r') as infile:
data = np.fromiter(items(infile))
View zeroing.py
# Make up some data in nested lists
strain_data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
# To make things a bit more readable, let's define a function that operates
# on a single list...
def zero(data):
"""Returns the difference between the items in "data" and its first item."""
# This is a "list comprehension". It's basically a 1-line for loop
return [item - data[0] for item in data]
You can’t perform that action at this time.