Created
July 25, 2018 21:51
-
-
Save howardhamilton/22fa91dfcb02cc8c055d4adf8d499953 to your computer and use it in GitHub Desktop.
Hinton diagram on a football pitch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as n | |
import pylab as p | |
from matplotlib.patches import Rectangle | |
from matplotlib.collections import PatchCollection | |
from pitch import create_normalized_pitch | |
def _add_centered_square(xy, area): | |
size = n.sqrt(area) | |
loc = n.asarray(xy) - size/2.0 | |
return Rectangle(loc, size, size) | |
def hinton(a, ax=None): | |
if ax is None: | |
ax = p.gca() | |
patches = [] | |
colors = [] | |
for xy, val in n.ndenumerate(a): | |
colors.append(val) | |
patches.append(_add_centered_square(n.asarray(xy) + 0.5, abs(val))) | |
patch_coll = PatchCollection(patches, cmap=p.get_cmap('plasma'), alpha=0.6) | |
patch_coll.set_array(n.array(colors)) | |
ax.add_collection(patch_coll) | |
ax.autoscale_view() | |
if __name__ == '__main__': | |
figure, axes = create_normalized_pitch(pcolor='white') | |
a = n.random.normal(loc=0.0, scale=0.2, size=(100, 100)) | |
hinton(a, ax=axes) | |
figure.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment