Revision: 8215
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8215&view=rev
Author:   ryanmay
Date:     2010-04-01 03:40:51 +0000 (Thu, 01 Apr 2010)

Log Message:
-----------
Add example of Hinton plots, which are used to plot weight matrices.

Added Paths:
-----------
    trunk/matplotlib/examples/api/hinton_demo.py

Added: trunk/matplotlib/examples/api/hinton_demo.py
===================================================================
--- trunk/matplotlib/examples/api/hinton_demo.py                                
(rev 0)
+++ trunk/matplotlib/examples/api/hinton_demo.py        2010-04-01 03:40:51 UTC 
(rev 8215)
@@ -0,0 +1,59 @@
+#Initial idea from David Warde-Farley on the SciPy Cookbook
+import numpy as np
+import matplotlib.pyplot as plt
+from matplotlib.patches import Rectangle
+from matplotlib.ticker import NullLocator
+#from matplotlib.collections import RegularPolyCollection
+#from matplotlib.colors import BoundaryNorm, ListedColormap
+
+def hinton(W, maxWeight=None, ax=None):
+    """
+    Draws a Hinton diagram for visualizing a weight matrix. 
+    """
+    if not ax:
+        fig = plt.figure()
+        ax = fig.add_subplot(1, 1, 1)
+
+    if not maxWeight:
+        maxWeight = 2**np.ceil(np.log(np.abs(W).max())/np.log(2))
+
+    ax.patch.set_facecolor('gray')
+    ax.set_aspect('equal', 'box')
+    ax.xaxis.set_major_locator(NullLocator())
+    ax.yaxis.set_major_locator(NullLocator())
+    cmap = ListedColormap(['black', 'white'])
+    norm = BoundaryNorm([-1., 0., 1.], cmap.N)
+
+    for (x,y),w in np.ndenumerate(W):
+        color = 'white' if w > 0 else 'black'
+        size = np.sqrt(np.abs(w))
+        rect = Rectangle([x - size / 2, y - size / 2], size, size,
+            facecolor=color, edgecolor=color)
+        ax.add_patch(rect)
+    ax.autoscale_view()
+    
+    # Reverse the yaxis limits
+    ax.set_ylim(*ax.get_ylim()[::-1])
+
+## Potential way using polygon collections that just has an issue with
+## easily getting the squares scaled by the data.
+
+#    height,width = W.shape
+#    x = np.arange(width)
+#    y = np.arange(height)
+#    X,Y = np.meshgrid(x, y)
+#    xy = np.array([X.flatten(),Y.flatten()]).T
+#    scaled_data = W.flatten() / maxWeight
+
+#    rect_col = RegularPolyCollection(4, rotation=np.pi/4,
+#        sizes=np.abs(scaled_data) * 72 / ax.figure.get_dpi(), offsets=xy,
+#        transOffset=ax.transData, norm=norm, cmap=cmap, edgecolor='none')
+#    ax.add_collection(rect_col)
+#    rect_col.set_array(scaled_data)
+#    ax.autoscale_view()
+
+if __name__ == '__main__':
+    hinton(np.random.rand(20, 20) - 0.5)
+    plt.title('Hinton Example')
+    plt.show()
+


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to