Wednesday, April 4, 2012

Plotting bars, whiskers and bridges in MATLAB (or, too much time spent getting the spacing just right)

I needed to plot the mean and variance of some data, and indicate pairs of data points which were significantly different.  I searched and searched, but there was no easy plot function in my environment of choice (MATLAB), so I made one myself.

The look of the plot is shown below; basically, you've got clusters of mean+variance data (or whatever you want to represent with a bar and whisker).  Within each cluster, there are pairwise relationships you want to indicate.  Relationships can be further specified by a variable number of marks above the bridges.
The function is here.  It takes three arguments: the first two are (number of clusters)-by-(number of bars per cluster), and represent (respectively) the height of the bars and the length of the whiskers.  The third argument is (number of bars per cluster)-by-(number of bars per cluster)-by-(number of clusters) and indicates whether a bridge should be drawn between a pair of bars; a nonzero value indicates that a bar should be drawn, and integers greater than one indicate that extra indication (in the form of circular marks) should be made above the bridges.

It's not the prettiest, but it works and it got the job done well enough for me.  Besides, any fine-tuning is going to be done in your vector editor of choice, so all that's important is getting the actors arranged on the stage.

No comments:

Post a Comment