Plotting data on binary axis and draw lines between points

Hi everyone,
I am trying to plot very simple data to better visualise the overall result. I've searched all I could think of, but I haven't been able to find any function able to do what I have in mind. I have 2 sets of data:
x1 = 2.6, 1.5, 1.4, 0.8, 3.2
y1 = 0.001, 0.9, 0.2, 0.5, 0.3
I want to plot both of these (x1 and y1) next to each other, on binary axes, using 'o' funtion for the data points and join the respective data points (i.e. 2.6 - 0.001, 1.5 - 0.9) with a line.
My problem, everything I tried so far hasn't worked or it re-arranges the axes and I dont know how to fix it. Please see the hand-drawn version of what I have in mind.
Any suggestions would be very appreciated!

 Accepted Answer

Perhaps something like this —
x1 = [2.6, 1.5, 1.4, 0.8, 3.2];
y1 = [0.001, 0.9, 0.2, 0.5, 0.3];
figure
hs = scatter([1 2],[x1; y1].', 'filled');
hold on
plot([1 2], [x1([1 2]); y1([1 2])], 'LineWidth',3)
hold off
set(gca, 'XTick',[1 2], 'XTickLabel',{'x_1','y_1'})
xline(1.5,'--k')
xlim([0.5 2.5])
.

6 Comments

I am so shocked... that is literally perfect!
I see you assigned x1 to a binary 1 and y1 to 2 ,which I haven't seen before, and later on when you set the x-axis limit; xlim([0.5 2.5]), the axes are easier to read.
I am super impressed, thank you very much for your help! :)
ps. this is the final graph!
As always, my pleasure!
Thank you!
My first post linked all the lines, then I saw that you only wanted the first two linked so my edit was to do just those.
@Vyte Jan you said "using 'o' funtion for the data points" and you showed hollow circles in your picture, so if you want that
  1. don't use the 'filled' option in scatter, or
  2. use 'o-' in plot instead of '.-'
Oh I didn't even see the edit come up, I was mesmeraized by the first graph!
I wanted all the points joined, but I thought it'd be easier to explain my vision by joining only the first two. If anyone comes across this post they will be able to see both options available.
Once again, thank you! :)
@Image Analyst I actually have no preference on the 'o' filled or hollow, it was more for conveniace! But that is good advise. I'll see what looks better when I have all the results.
Thank you!!
My first post (before the edit) was simply —
x1 = [2.6, 1.5, 1.4, 0.8, 3.2];
y1 = [0.001, 0.9, 0.2, 0.5, 0.3];
figure
plot([1 2], [x1; y1].', '.-', 'MarkerSize',25, 'LineWidth',3)
set(gca, 'XTick',[1 2], 'XTickLabel',{'x_1','y_1'})
xline(1.5,'--k')
xlim([0.5 2.5])
.

Sign in to comment.

More Answers (1)

Try this:
x1 = [2.6, 1.5, 1.4, 0.8, 3.2];
y1 = [0.001, 0.9, 0.2, 0.5, 0.3];
plotColors = jet(length(x1));
for k = 1 : length(x1)
plot([0.5, 1.5], [x1(k), y1(k)], '.-', ...
'Color', plotColors(k, :), 'LineWidth', 3,'MarkerSize', 50)
hold on;
end
grid on;
xline(1, 'LineStyle','--', 'Color', 'k', 'LineWidth',2)
xlim([0, 2]);
xticks([0 : 0.5 : 2])
xticklabels({[], 'x1', [], 'y1'})

Tags

Asked:

on 5 Sep 2022

Edited:

on 5 Sep 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!