MATLAB swarmchart in MATLAB®

Learn how to make 7 swarmchart charts in MATLAB, then publish them to the Web with Plotly.


Create Swarm Chart

Create a vector of x coordinates, and use the randn function to generate normally distributed random values for y. Then create a swarm chart of x and y.

x = [ones(1,500) 2*ones(1,500) 3*ones(1,500)];
y1 = 2 * randn(1,500);
y2 = 3 * randn(1,500) + 5;
y3 = 5 * randn(1,500) + 5;
y = [y1 y2 y3];
swarmchart(x,y)

fig2plotly()

Plot Multiple Data Sets with Custom Marker Size

Create three sets of x and y coordinates. Use the randn function to generate random values for y.

x1 = ones(1,500); x2 = 2 * ones(1,500); x3 = 3 * ones(1,500); y1 = 2 * randn(1,500); y2 = [randn(1,250) randn(1,250) + 4]; y3 = 5 * randn(1,500) + 5;

Create a swarm chart of the first data set, and specify a uniform marker size of 5. Then call hold on to plot the second and third data sets together with the first data set. Call hold off to release the hold state of the axes.

swarmchart(x1,y1,5)
hold on
swarmchart(x2,y2,5)
swarmchart(x3,y3,5)
hold off

fig2plotly()

Specify Marker Type

Read the BicycleCounts.csv data set into a timetable called tbl. This data set contains bicycle traffic data over a period of time. Display the first five rows of tbl.

tbl = readtable(fullfile(matlabroot,'examples','matlab','data','BicycleCounts.csv')); tbl(1:5,:)

ans=5×5 table
         Timestamp              Day         Total    Westbound    Eastbound
    ___________________    _____________    _____    _________    _________

    2015-06-24 00:00:00    {'Wednesday'}     13          9            4    
    2015-06-24 01:00:00    {'Wednesday'}      3          3            0    
    2015-06-24 02:00:00    {'Wednesday'}      1          1            0    
    2015-06-24 03:00:00    {'Wednesday'}      1          1            0    
    2015-06-24 04:00:00    {'Wednesday'}      1          1            0    

Create a vector `x` with the day name from each observation, and another vector y with the bicycle traffic observed. Then create a swarm chart of `x` and `y`, and specify the point marker `('.')`. The chart shows the distribution of bicycle traffic according to the day of the week.
daynames = ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"];
x = categorical(tbl.Day,daynames);
y = tbl.Total;
swarmchart(x,y,'.');

fig2plotly()

Display Filled Markers with Varied Color

Read the BicycleCounts.csv data set into a timetable called tbl. Create a vector x with the day name for each observation, another vector y with the bicycle traffic observed, and a third vector c with the hour of the day.

Then create a swarm chart of x and y, and specify the marker size as 20. Specify the colors of the markers as vector c. The values in the vector index into the figure's colormap. Thus, the colors change according to the hour for each data point. Use the 'filled' option to fill the markers with color instead of displaying them as hollow circles.

tbl = readtable(fullfile(matlabroot,'examples','matlab','data','BicycleCounts.csv'));
daynames = ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"];
x = categorical(tbl.Day,daynames);
y = tbl.Total;
c = hour(tbl.Timestamp);
swarmchart(x,y,20,c,'filled');

fig2plotly()

Change Jitter Type and Jitter Width

Read the BicycleCounts.csv data set into a timetable called tbl. Create a vector x with the day name for each observation, another vector y with the bicycle traffic observed, and a third vector c with the hour of the day. Then create a swarm chart of x and y, and specify the marker size as 5, and the colors of the markers as vector c. Call the swarmchart function with the return argument s, so that you can modify the chart after creating it.

tbl = readtable(fullfile(matlabroot,'examples','matlab','data','BicycleCounts.csv')); daynames = ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]; x = categorical(tbl.Day,daynames); y = tbl.Total; c = hour(tbl.Timestamp); s = swarmchart(x,y,5,c);

Change the shapes of the clusters at each x location, so that the points are uniformly and randomly distributed and the spacing is limited to no more than 0.5 data units.

s.XJitter = 'rand';
s.XJitterWidth = 0.5;

fig2plotly()

Specify Filled Markers with Transparency

Create a pair of x and y coordinates. Use the randn function to generate random values for y. Then create a swarm chart with filled markers that are 50% transparent both on their faces and on their edges.

x1 = ones(1,500);
x2 = 2 * ones(1,500);
x = [x1 x2];
y1 = 2 * randn(1,500);
y2 = [randn(1,250) randn(1,250) + 4];
y = [y1 y2];
swarmchart(x,y,'filled','MarkerFaceAlpha',0.5,'MarkerEdgeAlpha',0.5)

fig2plotly()

Specify the Target Axes

Read the BicycleCounts.csv data set into a timetable called tbl. This data set contains bicycle traffic data over a period of time. Display the first five rows of tbl.

tbl = readtable(fullfile(matlabroot,'examples','matlab','data','BicycleCounts.csv')); tbl(1:5,:)

ans=5×5 table
         Timestamp              Day         Total    Westbound    Eastbound
    ___________________    _____________    _____    _________    _________

    2015-06-24 00:00:00    {'Wednesday'}     13          9            4    
    2015-06-24 01:00:00    {'Wednesday'}      3          3            0    
    2015-06-24 02:00:00    {'Wednesday'}      1          1            0    
    2015-06-24 03:00:00    {'Wednesday'}      1          1            0    
    2015-06-24 04:00:00    {'Wednesday'}      1          1            0    

Define `x` as a categorical array of the day names in the table. Define `yEast` and `yWest` as vectors containing the eastbound and westbound bicycle traffic counts. daynames = ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]; x = categorical(tbl.Day,daynames); yEast = tbl.Eastbound; yWest = tbl.Westbound; Create a tiled chart layout in the `'flow'` tile arrangement, so that the axes fill the available space in the layout. Call the `nexttile` function to create an axes object and return it as `ax1`. Then create a swarm chart of the eastbound data by passing `ax1` to the `swarmchart` function.
tiledlayout('flow')
ax1 = nexttile;
y = tbl.Eastbound;
swarmchart(ax1,x,y,'.');

fig2plotly()

Repeat the process to create a second axes object and a swarm chart for the westbound traffic.

ax2 = nexttile; y = tbl.Westbound; s = swarmchart(ax2,x,y,'.');