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
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
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,'.');