필터 지우기
필터 지우기

Adding a colorbar to parallel coordinate plot

조회 수: 15 (최근 30일)
Eileen Lukens
Eileen Lukens 2021년 8월 26일
댓글: Jiri Junek 2022년 1월 11일
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',[])
Jiri Junek
Jiri Junek 2022년 1월 11일
It helped me too, thank you @Chunru.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by