Search code examples
matlabtextcolorsmatlab-figuresom

change color of SOM patch faces and text in MATLAB


I'm graphing a 20 x 20 SOM using the plotsomhits() function. A plot is automatically generated with white text (the number of input vectors in that neuron), overlaid on a blue patch (where the size of the patch corresponds to the number of observations/ input vectors in that neuron) on top of grid with a white background (representing the neurons). For the cells/neurons with small number of observations/input vectors the white text blends into the white cell background and is unreadable. I found a way to manually change each "patch" one by one using the figure GUI (View=>Property Editor=>click on each cell individually) but I would have to do that 400 times for this particular plot. Is there a better way to do this? Is there a way to change all of the text in all of the patches to be black so that the numbers are readable? Thanks you!

Picture_of_my_SOM.PNG

input1=randi([0 1], 50000, true);
input2=randi([0 1], 50000, true);
input3=randi([0 1], 50000, true);
input4=randi([0 100], 50000, true);
fakedata = [input1, input2, input3, input4];
D = 20;
dimensions = [D D];
coverSteps = 30;
initNeighbor = 3;
topologyFcn = 'gridtop';
distanceFcn = 'linkdist';
net1 = selforgmap(dimensions, coverSteps, initNeighbor, topologyFcn,distanceFcn);
net1 = train(net1, fakedata');

Solution

  • You can do this by getting all the child objects of the current axes, selecting only the text objects using findobj, and setting all of their colors in one step like so:

    set(findobj(get(gca, 'Children'), 'Type', 'Text'), 'Color', 'k');
    

    Here's an example, using the Fisher Iris data set:

    x = iris_dataset;
    net = selforgmap([5 5]);
    net = train(net, x);
    plotsomhits(net, x);
    set(findobj(get(gca, 'Children'), 'Type', 'Text'), 'Color', 'k');
    

    And you should get a plot similar to this:

    enter image description here

    If you'd also like to change the colors of the hexagonal patch objects (either the blue foreground or white background), you can get a vector of all the patch object handles, select either the first half (foreground patches) or last half (background patches), and set the FaceColor property to a given color value:

    patches = findobj(get(gca, 'Children'), 'Type', 'Patch');  % All the patch objects
    set(patches(1:25), 'FaceColor', 'r');   % Make foreground patches red
    set(patches(26:50), 'FaceColor', 'c');  % Make background patches cyan