Day 2: Have fun adding multiple dimensions to your data visualizations

Jozsef Meszaros
3 min readMay 5, 2020

--

Disclaimer: You should probably check out day 1 before you get started here.

Plotting gives us a new way to use arrayfun

The efficiency gains of arrayfun increase exponentially as your task gets more complex. Rather than designing extended for loops with multiple variables that clog up your workspace, you will save time on typing out code. Some of that time will be invested early on into thinking about your data and about your coding practices.

Here’s a simple example of how you could plot five data points at x=1 through 5, y = 5 random values, and with sizes set by the variable data:

rng(5) % Set the random numbers so your plot looks like mine
N = 5;
x = [1:N];
y = rand(1,N);
data = [20,30,35,50,40];
% Note always set up your figure area first
figure('color','w');
ax = axes('NextPlot','add','Xlim',[0,N],'Ylim',[0,1]);
arrayfun( @(i) plot(ax,x(i),y(i),'.','MarkerSize',data(i) ), [1:N] )

This format allows you to easily specify more dimensions as long as you know the parameter of the plot you wish to take advantage of to convey your point. For example, you could specify the color as well. This would be useful for example if you have two groups of experimental subjects.

rng(5)
N = 5;
x = [1:N];
y = rand(1,N);
data = [20,30,35,50,40];
colors = {'r','k','r','k','r'};
figure('color','w');
ax = axes('NextPlot','add','Xlim',[0,N+1],'Ylim',[0,1]);
% We set the Xlim to 0,6 so we have extra space for labels %
arrayfun( @(i) plot(ax,x(i),y(i),'.','MarkerSize',data(i),'Color', colors{i} ), [1:N] )

Once you have your plot set up and you understand the logic of arrayfun, adding bells and whistles is easy. Drop this line into the same script as above, and you will have annotated your plot with the data values.

arrayfun( @(i) text(ax,.2+x(i),y(i),sprintf('y=%1.2f (N=%i)',y(i),data(i)) ), [1:N] )

In just two lines of arrayfun you have taken control of the color, size, and location of your data points — and labeled them for closer inspection. This means that for each data point, you can convey at least 4 dimensions.

These visualizations can help you immensely when it comes to checking on the quality of clustering algorithms or trying to spot patterns in your data. But just as a recommendation, avoid trying to display more than 4 features of your data in one plot!

Scale up these data visualization skills by using subplot more intelligently. Go to day 3 to find out how.

This story is a part of my series titled ’30 Days of MATLAB tips I wish I had known doing graduate school in neuroscience’. Follow me here Neurojojo or on Twitter to stay updated with more tips.

--

--

Jozsef Meszaros
Jozsef Meszaros

No responses yet