Skip to content

Instantly share code, notes, and snippets.

View joferkington's full-sized avatar
🏠
Working from home

Joe Kington joferkington

🏠
Working from home
View GitHub Profile
import matplotlib.pyplot as plt
def main():
resized = (8, 3)
basic_example()
basic_example(resized)
fixed_aspect()
fixed_aspect(resized)
import numpy as np
import pandas as pd
#-- Generate some data similar to yours
idx = np.arange(20)
np.random.shuffle(idx)
idx1 = idx[:15]
np.random.shuffle(idx)
idx2 = idx[:10]
@joferkington
joferkington / silly.py
Created August 28, 2015 18:51
Lovely dynamic typing
import random
class BadIdea(object):
def __getattr__(self, key):
return random.randint(0, 1000)
def __setattr__(self, key, value):
pass
x = BadIdea()
import matplotlib.pyplot as plt
from matplotlib.mlab import csv2rec
from matplotlib.cbook import get_sample_data
#fname = get_sample_data('percent_bachelors_degrees_women_usa.csv')
fname = 'percent_bachelors_degrees_women_usa.csv'
gender_degree_data = csv2rec(fname)
# These are the colors that will be used in the plot
import random
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
def main():
accounts = generate_accounts()
lookup = plot(accounts)
datacursor(formatter=Formatter(lookup), bbox=dict(alpha=1))
plt.show()
import random
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
def main():
accounts = generate_accounts()
plot(accounts)
datacursor(formatter='Account #\n{label}'.format, bbox=dict(alpha=1))
plt.show()
@joferkington
joferkington / masked_hist.py
Created June 10, 2015 21:03
Quick example of masking 0-count portions of a 2D histogram
import numpy as np
import matplotlib.pyplot as plt
x, y = np.random.normal(0, 1, (2, 1000))
counts, ybins, xbins = np.histogram2d(y, x, bins=30)
counts = np.ma.masked_equal(counts, 0)
fig, ax = plt.subplots()
ax.pcolormesh(xbins, ybins, counts)
@joferkington
joferkington / point_drag_add_delete.py
Created March 26, 2015 13:46
General example of the type of framework you need to efficiently implement drawable/draggable/deleteable artists in matplotlib.
import numpy as np
import matplotlib.pyplot as plt
class DrawDragPoints(object):
"""
Demonstrates a basic example of the "scaffolding" you need to efficiently
blit drawable/draggable/deleteable artists on top of a background.
"""
def __init__(self):
self.fig, self.ax = self.setup_axes()
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
fig = plt.figure()
plot_extents = 0, 10, 0, 10
transform = Affine2D().rotate_deg(45)
helper = floating_axes.GridHelperCurveLinear(transform, plot_extents)
ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=helper)
@joferkington
joferkington / rose.py
Last active October 27, 2017 02:24
Correctly fix azimuths >360 or <0
import itertools
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
import numpy as np
np.random.seed(1977)
def main():
azi = np.random.normal(20, 40, 1000)