Adding a colorbar to parallel coordinate plot

18 views (last 30 days)
Hi all,
I am doing a parallel coordinate plot using the "parallelplot" plot command (example shown below). I have a range of 100 values as my "GroupVariable" and rather than have a 100 different categories for my legend, I just want to use a colorbar to show the gradient. I know I can plot in otherways to avoid this issue, but given that parallel coordinate plots can present and highlight data differently based on the arrangement of your factors and grouping strategies, I want to give a colorbar a try since I like this arrangement best for my data. Is there a way to add a colorbar legend easily? The only other option I can think of is doing two subplots, one with the parallel plotting command and another with a simple plotting command that will let me add a colorbar, and then supressing the second subplot and only showing the associated colorbar. But that's a little involved ,so I am hoping for a simpler solution.
Thanks in advanced!
%% generate numbers
a = randi([1,5],100,1);
b = randi([3,9],100,1);
c = randi([10,20],100,1);
d = rand(100,1);
%% make table
labels = {'a', 'b', 'c','d'};
T = table(a,b,c,d,'VariableNames',labels);
coordvars = {'a','b','c'};
%%sort rows for plotting
T_sort = sortrows(T,4);
%% plotting
p = parallelplot(T_sort,'CoordinateVariables',coordvars,'GroupVariable','d','DataNormalization','zscore');
p.Jitter = 0.3;
% assign color
p.Color = autumn;
%%%%%%%%%%% insert colorbar legend somehow instead of 100 different categories?? %%%%%%%%%%%%%

Accepted Answer

Chunru
Chunru on 27 Aug 2021
You can try to put a color bar in a subplot.
a = randi([1,5],100,1);
b = randi([3,9],100,1);
c = randi([10,20],100,1);
d = rand(100,1);
%% make table
labels = {'a', 'b', 'c','d'};
T = table(a,b,c,d,'VariableNames',labels);
coordvars = {'a','b','c'};
%%sort rows for plotting
T_sort = sortrows(T,4);
%% plotting
subplot(1,8, [1:7])
p = parallelplot(T_sort,'CoordinateVariables',coordvars,'GroupVariable','d','DataNormalization','zscore');
p.Jitter = 0.3;
% assign color
p.Color = hsv;
p.LegendVisible = 'off';
subplot(1,8,8);
ncolors = size(p.Color, 1);
image(1, 1:ncolors, (1:ncolors)'); axis xy
colormap(p.Color);
  2 Comments
Eileen Lukens
Eileen Lukens on 27 Aug 2021
Edited: Eileen Lukens on 27 Aug 2021
Thank you! This was super helpful!! It was very close to what I wanted. I had to adjust it a little to work for my purposes since my actual data set is set up a bit different. I needed to scale the colorbar to my data set range in the end. In case anyone runs into the same thing, the final code that worked best for my specific data set is below. However, for the example I provided, @Chunru's solution works best. Thanks!
a = randi([1,5],100,1);
b = randi([3,9],100,1);
c = randi([10,20],100,1);
d = rand(100,1);
%% make table
labels = {'a', 'b', 'c','d'};
T = table(a,b,c,d,'VariableNames',labels);
coordvars = {'a','b','c'};
%%sort rows for plotting
r = 4;
T_sort = sortrows(T,r);
%% plotting
subplot(1,8, [1:7])
p = parallelplot(T_sort,'CoordinateVariables',coordvars,'GroupVariable','d','DataNormalization','zscore');
p.Jitter = 0.3;
p.LegendVisible = 'off';
% assign color
p.Color = hsv(length(table2array(T_sort(:,r))));
%define CData
scale_data = table2array(T_sort(:,r));
%formatting colorbar
subplot(1,8,8);
m = image('XData',[0 1],'YData',scale_data,...
'CData',flipud(scale_data),'CDataMapping','scaled');
axis xy
colormap(flipud(p.Color))
ylim([min(scale_data),max(scale_data)])
xlim([0,1])
set(gca,'xtick',[])

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!