Skip to content

Commit 54eaa89

Browse files
committed
Cleanup and move hinton_demo
1 parent 5c7b6c4 commit 54eaa89

File tree

2 files changed

+41
-60
lines changed

2 files changed

+41
-60
lines changed

examples/api/hinton_demo.py

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Demo of a function to create Hinton diagrams.
3+
4+
Hinton diagrams are useful for visualizing the values of a 2D array (e.g.
5+
a weight matrix): Positive and negative values are represented by white and
6+
black squares, respectively, and the size of each square represents the
7+
magnitude of each value.
8+
9+
Initial idea from David Warde-Farley on the SciPy Cookbook
10+
"""
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
14+
15+
def hinton(matrix, max_weight=None, ax=None):
16+
"""Draw Hinton diagram for visualizing a weight matrix."""
17+
ax = ax if ax is not None else plt.gca()
18+
19+
if not max_weight:
20+
max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2))
21+
22+
ax.patch.set_facecolor('gray')
23+
ax.set_aspect('equal', 'box')
24+
ax.xaxis.set_major_locator(plt.NullLocator())
25+
ax.yaxis.set_major_locator(plt.NullLocator())
26+
27+
for (x,y),w in np.ndenumerate(matrix):
28+
color = 'white' if w > 0 else 'black'
29+
size = np.sqrt(np.abs(w))
30+
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
31+
facecolor=color, edgecolor=color)
32+
ax.add_patch(rect)
33+
34+
ax.autoscale_view()
35+
ax.invert_yaxis()
36+
37+
38+
if __name__ == '__main__':
39+
hinton(np.random.rand(20, 20) - 0.5)
40+
plt.show()
41+

0 commit comments

Comments
 (0)