In one of my previous questions, the magnificent Jake helped me generate a heatmap that looks identical to using imagesc on a 2D matrix within MATLAB.
The results look great, but now I'm looking to modify its behaviour in order to create a Hinton diagram. A Hinton diagram is a cool way of visualising the magnitudes and directions of the weights in a neural network. Instead of using a colourmap to decide the colour of each "pixel" (e.g., black is -1, white is 1 and anything in between is a scale of grey), it uses colours x and y for negative and positive values respectively, and the magnitude is visualised by the size of the square marker. Below is an example output from a Python interpretation (the code might inspire someone).

Ideally, it would be possible to tuck all this away in a style.
As a bonus, it'd be great to have the ability to read a file that stores the data in a matrix's natural representation:
2.76658 -0.07990 0.10551 -2.94131 0.00840 0.23832 -2.56759 0.04593
-0.21476 2.37350 -2.30670 0.11634 -2.36124 2.62034 -0.32261 2.36860
0.01118 -2.42926 2.42470 0.08698 2.45065 -2.39544 -0.16931 -2.32933
-3.04568 -0.15376 0.03051 2.74830 -0.07237 0.02359 2.96758 0.05812
-0.12791 -2.50370 2.63524 0.15000 2.26310 -2.39198 -0.05032 -2.41050
-0.07663 2.40350 -2.45346 -0.00479 -2.62160 2.29896 -0.11746 2.49031
-2.90385 -0.11742 -0.15037 2.88956 0.01517 -0.06700 2.96463 -0.10442
-0.17761 2.01661 -2.23660 -0.07113 -2.39688 2.19306 0.07902 2.37361
Or, if that's not feasible, in a traditional x-y-data format like this:
0 0 2.766580
1 0 -0.079900
2 0 0.105510
3 0 -2.941310
4 0 0.008400
5 0 0.238320
6 0 -2.567590
7 0 0.045930
0 1 -0.214760
1 1 2.373500
2 1 -2.306700
3 1 0.116340
4 1 -2.361240
5 1 2.620340
6 1 -0.322610
7 1 2.368600
0 2 0.011180
1 2 -2.429260
2 2 2.424700
3 2 0.086980
4 2 2.450650
5 2 -2.395440
6 2 -0.169310
7 2 -2.329330
0 3 -3.045680
1 3 -0.153760
2 3 0.030510
3 3 2.748300
4 3 -0.072370
5 3 0.023590
6 3 2.967580
7 3 0.058120
0 4 -0.127910
1 4 -2.503700
2 4 2.635240
3 4 0.150000
4 4 2.263100
5 4 -2.391980
6 4 -0.050320
7 4 -2.410500
0 5 -0.076630
1 5 2.403500
2 5 -2.453460
3 5 -0.004790
4 5 -2.621600
5 5 2.298960
6 5 -0.117460
7 5 2.490310
0 6 -2.903850
1 6 -0.117420
2 6 -0.150370
3 6 2.889560
4 6 0.015170
5 6 -0.067000
6 6 2.964630
7 6 -0.104420
0 7 -0.177610
1 7 2.016610
2 7 -2.236600
3 7 -0.071130
4 7 -2.396880
5 7 2.193060
6 7 0.079020
7 7 2.373610
You can generate the second data format from the first using a Python script like this:
import numpy as np
rawdata = np.loadtxt('data.dat')
coords = np.meshgrid(np.arange(np.shape(rawdata)[0]),
np.arange(np.shape(rawdata)[1]))
data = np.vstack([coords[0].flatten(), coords[1].flatten(), rawdata.flatten()]).T
np.savetxt('weights-example.dat', data, fmt="%i %i %f")
This is probably a bit too much to ask of the TikZ/pgfplots gurus as I have not really put any effort into solving this, but, alas, I cannot even fully understand how Jake's code works. If anyone's willing to help, I guess Jake's answer to my previous question would be the place to start.




pgfplotsprovides the capability to normalise the data on the go. (2) I think it represents the area of the square. I could be wrong though! That' why I linked to the Python code!:-)– sudosensei Jan 21 '14 at 21:53pgfbackend ofmatplotliband export the diagram to pgf code, which you can then\inputinto your latex document. Would that be acceptable? – Psirus Jan 21 '14 at 22:38