Skip to content

Instantly share code, notes, and snippets.

@znstrider
znstrider / fig_camera.py
Created December 19, 2020 12:42
store figure snapshots to list and make gifs
from PIL import Image
import io, imageio
from matplotlib.figure import Figure
class Camera:
"""
Adaptation from https://github.com/jwkvam/celluloid/blob/master/celluloid.py
This makes complete snapshots of the figure, not only the new artists
"""
@znstrider
znstrider / camera.py
Created December 19, 2020 12:42
celluloid with ability to show all artists
from typing import Dict, List
from collections import defaultdict
from matplotlib.figure import Figure
from matplotlib.artist import Artist
from matplotlib.animation import ArtistAnimation
class Camera:
"""Make animations easier.
@znstrider
znstrider / gs_from_array.py
Last active February 18, 2020 17:50
easy creation of matplotlib.gridspec plots
def gs_from_array(layout, figsize = None, **kwargs):
"""
makes a matplotlib.gridspec from a layout given by an array.
ie:
layout = np.array([[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 0, 2, 2]])
will create a 3x4 Gridspec with:
@znstrider
znstrider / htext.py
Created February 11, 2020 14:32
highlight text - functions based on plt.text and fig.text to allow for <color highlighted substrings>.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def htext(s, x, y,
highlight_colors = ['C0'],
color = 'w',
ax = None,
delim = ['<', '>'],
hpadding = 0,
@znstrider
znstrider / gist:a52147584908f1e5413844204688fbe1
Created February 10, 2020 13:10
matplotlib function to make text objects with color highlighted substrings
def colored_text_ax(s, x, y,
color = 'w',
ax = None,
xlim = None,
highlight_colors = ['C0'],
hpadding = 0,
**kwargs):
'''
takes a string with substrings in {} to be highlighted according to highlight colors:
'The weather is {sunny} today. Yesterday it {rained}.', color = 'w', highlight_colors = ['yellow', 'grey']