#
Violin Plots
in
MATLAB^{®}

How to make Violin plots in MATLAB^{®} 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(gcf);
```

## 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.

```
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;
swarmchart(x1,y1,5)
hold on
swarmchart(x2,y2,5)
swarmchart(x3,y3,5)
hold off
fig2plotly(gcf);
```

## 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.

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.

```
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;
swarmchart(x,y,'.');
fig2plotly(gcf);
```

## 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(gcf);
```

## 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);
fig2plotly(gcf);
```