swarmchart3Read 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.
daynames = ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]; x = categorical(tbl.Day,daynames);
Create a categorical vector
ycontaining the values"pm"or"am"according to the time for each observation in the table. Create vectorzof eastbound traffic data. Then create a swarm chart ofx,y, andz. The chart shows the data distributions for each morning and evening of the week.
ispm = tbl.Timestamp.Hour < 12; y = categorical; y(ispm) = "pm"; y(~ispm) = "am"; z= tbl.Eastbound; swarmchart3(x,y,z); fig2plotly()
Create vector
xas a combination of zeros and ones, and createyas a vector containing all ones. Createzas a vector of squared random numbers. Then create a swarm chart ofx,y, andz, and specify the size marker size as5.
x = [zeros(1,500) ones(1,500)]; y = ones(1,1000); z = randn(1,1000).^2; swarmchart3(x,y,z,5); fig2plotly()
Create vector
xas a combination of zeros and ones, and createyas a vector containing all ones. Createzas a vector of squared random numbers. Then create a swarm chart ofx,y, andz, and specify the point ('.') marker symbol.
x = [zeros(1,500) ones(1,500)]; y = ones(1,1000); z = randn(1,1000).^2; swarmchart3(x,y,z,'.'); fig2plotly()
Create vector
xcontaining a combination of zeros and ones, and createycontaining a random combination of ones and twos. Createzas a vector of squared random numbers. Specify the colors for the markers by creating vectorcas the square root ofz. Then create a swarm chart ofx,y, andz. Set the marker size to50and specify the colors asc. The values incindex into the figure's colormap. Use the'filled'option to fill the markers with color instead of displaying them as hollow circles.
x = [zeros(1,500) ones(1,500)]; y = randi(2,1,1000); z = randn(1,1000).^2; c = sqrt(z); swarmchart3(x,y,z,50,c,'filled'); fig2plotly()
Create vector
xcontaining a combination of zeros and ones, and createycontaining a random combination of the numbers one through four. Createzas a vector of squared random numbers. Then create a swarm chart ofx,y, andzby calling theswarmchartfunction with a return argument that stores theScatterobject. Add x- and y-axis labels so you can see the effect of changing the jitter properties in each dimension.
x = [zeros(1,500) ones(1,500)];
y = randi(4,1,1000);
z = randn(1,1000).^2;
s = swarmchart3(x,y,z);
xlabel('X')
ylabel('Y')
fig2plotly()
Change the shapes of the clusters of points by setting the jitter properties on the
Scatterobject. In thexdimension, specify uniform random jitter, and change the jitter width to0.5data units. In theydimension, specify normal random jitter, and change the jitter width to0.1data units. The spacing between points does not exceed the jitter width you specify.
s.XJitter = 'rand'; s.XJitterWidth = 0.5; s.YJitter = 'randn'; s.YJitterWidth = 0.1; 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 vector
xwith the days names for each observation. Create a categorical vectorycontaining the values"pm"or"am"according to the time for each observation in the table. Definezeas a vector of eastbound traffic data, and definezwas a vector of westbound traffic data.
daynames = ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]; x = categorical(tbl.Day,daynames); ispm = tbl.Timestamp.Hour<12; y = categorical; y(ispm) = 'pm'; y(~ispm) = 'am'; ze = tbl.Eastbound; zw = tbl.Westbound; fig2plotly()
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;
swarmchart3(ax1,x,y,ze,'.');
fig2plotly()
Repeat the process to create a second axes object and a swarm chart for the westbound traffic.
ax2 = nexttile; z = tbl.Westbound; swarmchart3(ax2,x,y,zw,'.'); fig2plotly()