joyPlot documentation
The joyPlot function plots your data in a ridgeline representation, just like in the cover of Joy Divisions' Unknown Pleasures (and in many other newspapers, journals, etc.). The function is very simple and only uses the MATLAB® built-in functions fill and plot. I am actually amazed that this wasn't programmed before, but there you go.
Contents
Syntax
joyPlot(data,x,offset)
joyPlot(data,x,offset,overlapMethod)
joyPlot(data,x,offset,overlapMethod,reverse)
joyPlot(_,Name,Value)
[hf,hl] = joyPlot(_)
[hs,hf,hl] = joyPlot(_)
[hs,hf,hl,hvl] = joyPlot(_)
Description
joyPlot(data,x,offset) Plots the information in the m by n matrix data, where n is the number of datasets and m is the sampling. x is a vector containing the
-coordinates of data. offset is the relative vertical distance between datasets. By default, the first row is plotted at the top and the last one at the bottom.
joyPlot(data,x,offset,overlapMethod) If overlapMethod is set to 'variable', offset is the relative overlap in percent between datasets.
joyPlot(data,x,offset,overlapMethod,reverse) If reverse is set to 'true', the first row of data will be plotted at the bottom and the last at the bottom.
joyPlot(_,Name,Value) Specifies patch and line properties using one or more 'Name,Value' pairs. For a list see Properties below.
[hf,hl] = joyPlot(_) Returns the patch and line handles.
[hs,hf,hl] = joyPlot(_) If the stroke is requested, its handle can be returned as well.
[hs,hf,hl,hvl] = joyPlot(_) If additional vertical lines are plotted, their handles can be returned too.
Examples
Joy Division's Unknown Pleasures album cover by Peter Saville
You can find online a LOT of information about this, so here is a link if you want to read the back story. I found the dataset (or at least a similar one) online. More details here and here .
% Save the data from the web filename = websave('pulsar.csv',['https://gist.githubusercontent.com/borgar/',... '31c1e476b8e92a11d7e9/raw/0fae97dab6830ecee185a63c1cee0008f6778ff6/',... 'pulsar.csv']); % Import it into MATLAB(R) data = readmatrix(filename); x = linspace(0,93,size(data,2)); % Create the figure and show the magic figure joyPlot(data',x,4) set(gcf,'position',[500,100,560,680]) set(gca,'Visible','off', 'box','off','XTick',[],'YTick',[])
Add some customization
Don't get me wrong, I don't think that this piece of art needs any more color, detail or anything. But for the sake of showing off the algorithm let's add some options.
% Add face colors as a function of the mean value of each dataset figure joyPlot(data',x,4,'FaceColor',mean(data,2)) set(gcf,'position',[500,100,560,680]) set(gca,'Visible','off', 'box','off','XTick',[],'YTick',[]) % Add a colorbar to show the propertiy value colorbar
One last example with this data set: change the line color and add a stroke. Why anyone would do something like this is beyond me, though.
figure joyPlot(data',x,4,'FaceColor',mean(data,2),'StrokeColor','k','LineColor','w') set(gcf,'position',[500,100,560,680]) set(gca,'Visible','off', 'box','off','XTick',[],'YTick',[])
Plot some distributions
Let's face it, we are not goint to plot interesting things like that pulsar. Let's go with some simple distributions. I will use this example to show how to reverse the data representation and use variable offset of the datasets.
% Create the datasets from normal distributions % Number of datasets n = 10; % Sampling N = 100; % Min and max values in x xmin = 0; xmax = 18; x = linspace(xmin,xmax,N); % Allocate a matrix to store the dataset data = zeros(N,n); % Create the distributions and the data with changing shape and scale. distName = 'gamma'; a = linspace(2,7,n); b = linspace(0.5,1.5,n); for ii = 1:n dist = makedist(distName,'a',a(ii),'b',b(ii)); data(:,ii) = pdf(dist,x); end figure % Set overlapMethod to 'variable' and reverse to 'true'. joyPlot(data,x,0.4,'variable',true,'FaceColor','k','StrokeColor','w') % Some axes settings set(gca,'Color',[0.93 0.93 0.93]) set(gca,'XGrid','on')
Other useful combinations
If the overall shape of a distribution is important but its maximum value, normalizing the data is the way to go. We will use the same dataset as before, but we will normalize it.
% Normalize the data data = normalize(data,1,'range'); % Plot with the position as color figure joyPlot(data,x,0.3,'variable',true,'FaceColor','position','FaceAlpha',0.6) % Some axes settings set(gca,'Color',[0.93 0.93 0.93]) set(gca,'XGrid','on') % Change the colormap, you know, for fun. colormap(spring)
Other possibility is to change the y-axis labels
% Create a char array with alphabet alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; % Formatting ylbls = sprintfc('Data %c',alph(1:n)); % Apply yticklabels(ylbls)
Modify plots after creation
We will also add modal indicators. For the gamma distribution the mode is computed like this: 
% Compute the modes M = (a-1).*b; % Insert them in the joyPlot and return the handles of all objects % By defining the offset as a negative number with overlapMethod % 'variable', the plots now are apart. figure [hf,hl,hvl] = joyPlot(data,x,-0.2,'variable',true,'FaceColor',M,'VLines',M); % Change the colormap colormap('autumn') % Now change the vertical line style set(hvl,'LineStyle','-.') % And the line color set(hl,'Color',[0.5,0.5,0.5]) % And you could keep going with all possible patch and line methods. %
Name-Value Pair Arguments
Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.
- FaceColor: works just like the color in the patch function. It additionally accepts the input 'position', which colors the faces according to the position in the y-axis.
- FaceAlpha: works just like 'FaceAlpha' in the function patch.
- LineColor: works just like the color of function plot.
- LineWidth: works just like 'LineWidth' in the function plot.
- StrokeColor: adds a stroke to each dataset with the specified color. Here, the color works just like in the funciton patch.
- StrokeWidth: adds a stroke to each dataset with the specified width. Again, this works like in the function patch.
- VLines: adds vertical lines in each dataset corresponding to the x-coordinates provided. This is useful to plot median, mean or mode values for example.
-VLinesColor: works just like the color of function plot.
-VLinesWidth: works just like 'LineWidth' in the function plot.
See also
plot, patch, fill
About
Programmed by Santiago M. Benito at the Chair of Materials Technology of the Ruhr-Universität Bochum.