swarmchartCreate a vector of
xcoordinates, and use therandnfunction to generate normally distributed random values fory. Then create a swarm chart ofxandy.
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()
Create three sets of
xandycoordinates. Use therandnfunction to generate random values fory.
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 callhold onto plot the second and third data sets together with the first data set. Callhold offto release the hold state of the axes.
swarmchart(x1,y1,5) hold on swarmchart(x2,y2,5) swarmchart(x3,y3,5) hold off fig2plotly()
Read the
BicycleCounts.csvdata set into a timetable calledtbl. This data set contains bicycle traffic data over a period of time. Display the first five rows oftbl.
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
xwith the day name from each observation, and another vector y with the bicycle traffic observed. Then create a swarm chart ofxandy, 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()
Read the
BicycleCounts.csvdata set into a timetable calledtbl. Create a vectorxwith the day name for each observation, another vectorywith the bicycle traffic observed, and a third vectorcwith the hour of the day.Then create a swarm chart of
xandy, and specify the marker size as20. Specify the colors of the markers as vectorc. 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()
Read the
BicycleCounts.csvdata set into a timetable calledtbl. Create a vectorxwith the day name for each observation, another vectorywith the bicycle traffic observed, and a third vectorcwith the hour of the day. Then create a swarm chart ofxandy, and specify the marker size as5, and the colors of the markers as vectorc. Call theswarmchartfunction with the return arguments, 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); fig2plotly()
Change the shapes of the clusters at each
xlocation, so that the points are uniformly and randomly distributed and the spacing is limited to no more than0.5data units.
s.XJitter = 'rand'; s.XJitterWidth = 0.5; fig2plotly()
Create a pair of
xandycoordinates. Use therandnfunction to generate random values fory. 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()
Read the
BicycleCounts.csvdata set into a timetable calledtbl. This data set contains bicycle traffic data over a period of time. Display the first five rows oftbl.
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
xas a categorical array of the day names in the table. DefineyEastandyWestas 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 thenexttilefunction to create an axes object and return it asax1. Then create a swarm chart of the eastbound data by passingax1to theswarmchartfunction.
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,'.'); fig2plotly()