Plot with 3 Variables

5 views (last 30 days)
Gillian Murray
Gillian Murray on 5 Jul 2021
Answered: Star Strider on 5 Jul 2021
I have a table with 3 variables that I would like to put into a plot. I have variable X that I want on the X-axis, variable Y that I want on the Y-axis, and then a third variable titled 'Activity'. Is it possible to plot the X and Y as normal and add the third variable through color coding with a key? I'm open to other ideas as well! Thanks in advance!

Accepted Answer

Star Strider
Star Strider on 5 Jul 2021
If I understand correctly, the scatter function (or a combination of scatter and plot if you want the points connnected with lines) would likely work.
Example —
T1 = array2table([rand(20,2) randi(4, 20, 1)], 'VariableNames',{'X','Y','Activity'})
T1 = 20×3 table
X Y Activity ________ _________ ________ 0.75828 0.94616 1 0.031499 0.22264 4 0.10177 0.94944 1 0.32075 0.2479 2 0.18952 0.0022698 4 0.60777 0.99533 1 0.2849 0.9708 4 0.10925 0.36814 3 0.95879 0.31984 4 0.69451 0.19754 4 0.060238 0.38941 4 0.81184 0.44144 1 0.41601 0.73352 4 0.22987 0.73767 1 0.80872 0.18386 3 0.64017 0.52881 4
Ua = unique(T1.Activity);
figure % Specific Number Of Unique 'Activity' Values & Legend Entries
hold on
for k = 1:numel(Ua)
vk = T1.Activity == k;
hs{k} = scatter(T1.X(vk,:), T1.Y(vk,:), 25, T1.Activity(vk,:),'filled');
end
hold off
grid
colormap('turbo')
legend([hs{:}],compose('Activity %d',Ua), 'Location','best')
figure % Any Number Of Activity Values, Optionally Connectyed By Lines, No Specific LEgend
scatter(T1.X, T1.Y, 25, T1.Activity,'filled')
hold on
plot(T1.X, T1.Y, ':k')
hold off
grid
colormap('turbo')
Make appropriate changes to get the result you want
.

More Answers (1)

Image Analyst
Image Analyst on 5 Jul 2021
Sure
plot(t.X, t.Y, 'b.-'); % t is your table variable.
Not sure what the third variable is or how you want it to appear on the x-y graph. Please explain and attach your table in a .mat file
save('answers.mat', 'yourTable');

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!