MATLAB Answers

Connecting points between two box plots

22 views (last 30 days)
Rose Potter
Rose Potter on 26 Aug 2019
Commented: darova on 26 Aug 2019
Hello everyone,
I attached a picture of what I'm trying to plot to make it clear. I'm using the function notBoxPlot to create these "boxplots" and now I'd like to connect each point from the first boxplot to its counterpart in the second and third boxplot. I was told that I can use the command "line", however I could not figure it out despite reading the documentation and questions about connecting points (none of them seem to fit my problem).
I tried this for two boxplots but it does not yield any connecting lines at all. The vectors y1 and y2 are simply column vectors with 30 numbers each.
y1 = [CSPlus_shock]
y2 = [CSPlus_noshock]
notBoxPlot([y1,y2],[1:2])
hold on
line(y1,y2)

Accepted Answer

the cyclist
the cyclist on 26 Aug 2019
Edited: the cyclist on 26 Aug 2019
I did not download notBoxPlot, but here is an example using a scatter plot instead.
% Some pretend data
N = 5;
x = [randn(N,1); 100+randn(N,1)]; % 5 points toward the left of the plot, and 5 toward the right
y = sort(randn(2*N,1));
% Scatter plot, and then connect each marker on the left
% to corresponding one on the right
figure
scatter(x,y)
line([x(1:N) x(N+1:end)]',[y(1:N) y(N+1:end)]')
I think the key to understanding how this works is to look at the array arguments to the line command.
There are 5 pairs of points in the plot. Each of the two argument to line is a 2x5 array:
line([2x5 array of x points],[2x5 array of y points])
Each column of that array has 2 values: (1) the x value of the left-hand point, and (2) the x value of the right-hand point. And similarly for the second argument of the line command, but using the y values.
If that's still a bit confusing, check out this syntax, which only draws one line between two points.
% Some pretend data
N = 5;
x = [randn(N,1); 100+randn(N,1)];
y = sort(randn(2*N,1));
% Scatter plot, and then connect *one* marker on the left
% to corresponding one on right
figure
scatter(x,y)
line([x(1) x(N+1)]',[y(1) y(N+1)]')
If you wanted to connect 5 sets of 3 data points, your line command would be
line([3x5 array of x points],[3x5 array of y points])

  1 Comment

Rose Potter
Rose Potter on 26 Aug 2019
Ahhh this works! Thank you so much! And such a great detailed explanation, thank you!

Sign in to comment.

More Answers (0)


Translated by