Adding a colorbar to parallel coordinate plot

14 次查看(过去 30 天)
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?? %%%%%%%%%%%%%

采纳的回答

Chunru
Chunru 2021-8-27
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 个评论
Eileen Lukens
Eileen Lukens 2021-8-27
编辑:Eileen Lukens 2021-8-27
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',[])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by