Friends,
In the mpl site, i found the an example code called hinton_demo to plot
weighted matrix in the following link.

http://matplotlib.sourceforge.net/examples/api/hinton_demo.html?highlight=hinton

I used the same script (attached test.py) making a slight modification in
the following section of the code pasted below. I just included more color
range and modified the size as np.sqrt(np.abs(w))/5.

However when i run the script with test data (attached test.dat), i found
that the size of the square and color are not matching. For instance the
size of the squares at position 4,3 and 7,6 are small but the color (black)
is not relevant to the size. I am not getting what is going wrong. Kindly
help to resolve the problem.

The script and test data are attached.

*Section of the code modified*

for (x,y),w in np.ndenumerate(W):
        if w > 0: color = 'white'
        else:     color = '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)
#!/usr/bin/env python   
#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
import matplotlib as mpl
from sys import argv
# This script was extraced from mpl website.
mpl.rcParams['font.size']=7
mpl.rcParams['font.family']='bold'


mat=np.loadtxt(argv[1])

def hinton(W, maxWeight=None, ax=None):
    """
    Draws a Hinton diagram for visualizing a weight matrix.
    """
    if not ax:
        fig = plt.figure(figsize=(3.60,3.60),dpi=300)
        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('white')                           
    ax.set_aspect('equal', 'box')
    ax.xaxis.set_major_locator(NullLocator())
    ax.yaxis.set_major_locator(NullLocator())

    for (x,y),w in np.ndenumerate(W):
        if   w < 2.0: color = 'red'
        elif 2.1 < w < 4.0: color='blue'
        elif 4.1 < w < 6.0: color='green'
        else: color = 'black'


        size = np.sqrt(np.abs(w))/5         
        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])
    

hinton(mat)
plt.xticks(np.arange(0,9,1)) 
plt.yticks(np.arange(0,9,1)) 
plt.ylim([-0.5,8.5])
plt.xlim([-0.5,8.5])
plt.savefig('test.png',dpi=300,bbox_inches='tight')
#plt.show()

Attachment: test.dat
Description: Netscape Proxy Auto Config

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to