Wednesday, February 8, 2012

Plotting three variable graph using Matlab

Recently, a user wanted to visualize the effect of four different test conditions causing changes in three different parameters.  This visualization will help understand the effect of change in one parameter on others.

The user suggested to plot the three parameters along three different axis.  For example, the three parameters with values of [95.0, 1.2, 4.5] will correspond to the co-ordinates of [95.0, 0.0, 0.0], [0, 1.2, 0] and [0, 0, 4.5].  Using these coordinates, one can form a triangle.  The shape of the triangle will be different for the various test conditions and it will be easier to visualize the effect of the test conditions on the parameters.

I was not initially sure whether I could accomplish this using a standard Matlab plots.  I did a search on google but was not successful, as I did not have a good search term.  I resorted to creating a OpenGL program using GL_TRIANGLES.   I later found that Matlab has similar functionality.  Triangles and other polygons can be easily constructed in Matlab using the "patch" function.

The program

The vals contains the value that needs to be plotted.  Each column is one test condition and the rows contain the parameters that need to be plotted along the axis.  The "for loop" runs for each column and creates the x, y and z coordinates and stores them in a, b and c.  The patch command will create a triangle using the three coordinates.  The last parameter in the patch command is the color of the patch.  By default, patch is rendered with all surface opaque and the color specified in the patch command.  Since there are too many surfaces, the patch was made transparent and the edges were given different line style and thickness using "plottools".  The resulting plot can be seen below.

% The variables vals has three rows and four columns. The columns contain co-ordinate values along x,y,z axis respectively.  The four rows will result in four triangular surfaces.
vals = [11.11,3.55,4.97,2.14;

hold on;
for i = 1:size(vals,2) % For each column in vals
   a = [vals(1,i)     0            0]
   b = [0         vals(2,i)        0]
   c = [0             0       vals(3,i)]
grid on;

No comments: