Skip to content

Instantly share code, notes, and snippets.

@mattpitkin
mattpitkin / swigtesting.md
Last active March 15, 2023 10:55
Adding python functionality to a SWIG wrapped structure

Adding python functionality to a SWIG-wrapped structure

This is a little example of adding some python-object-like functionality to a C structure that is SWIG-wrapped for use in python. What I've done mainly comes from this StackOverflow question, and the very useful answer, but is written here to remind me.

Say you have some C code containing a structure like this:

/* testswig.h file */
#include <stdlib.h>
#include <stdio.h>
@mattpitkin
mattpitkin / examplenetworkgrid.py
Last active May 22, 2023 11:55
A network plot of 2x2 grids
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
fig, ax = plt.subplots()
def draw_2x2_grid(ax, xy, bwidth=0.25, bheight=0.25, numbers=[]):
for i in range(2):
@mattpitkin
mattpitkin / colourfillviolin.md
Last active February 20, 2024 16:31
Violin plot with gradient colour fill

In answer to this StackOverflow question, here is a method (based heavily on this answer) to create a violin plot with a gradient colour fill based on the density of the samples (i.e., width of the violin):

from matplotlib import pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import matplotlib as mpl