Plot some part of a correlation matrix

I need help please as I have the following correlation matrix, and would like to plot only the lower left part as the upper part is just a mirror of the upper part. As bellow:
I used the following command:
% your example code
Fields = [1, 4, 5];
Fields_time = Fields +16;
MT_All = rand(100,26);
% MT_All = rand(100,9);
VariableNames={'sigma','sigma','sigma','tau','tau','tau','mNPV'}; % changed to ensure valid syntax
Mat_All_1_4_5 = [MT_All(:,Fields), MT_All(:,Fields_time), MT_All(:,end-1)];
figure
c = corrplot(Mat_All_1_4_5, 'varNames', VariableNames);
% get current figure handle
fh = gcf;
% find x and y label strings that are not empty within subplots
yLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.YLabel.String),fh.Children,'UniformOutput',false)));
xLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.XLabel.String),fh.Children,'UniformOutput',false)));
% rename y labels
for ik = 1:length(yLabelN)
if ik <= 3
fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
elseif ik <=6
fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
else
fh.Children(yLabelN(ik)).YLabel.String = sprintf('m_{NPV}');
fh.Children(xLabelN(ik)).XLabel.String = sprintf('m_{NPV}');
end
end

 Accepted Answer

Mehmed Saad
Mehmed Saad on 10 May 2020
Edited: Mehmed Saad on 10 May 2020
Access all objects with tag 'PlotMatrixScatterAx' using findobj
ax = findobj(fh,'Tag','PlotMatrixScatterAx');
  1. Run for loop for all axes and access only thoses axes which have two childrens
  2. Access the 2nd's children's Xdata and Ydata ( the scatter which is actually line plot with marker 'o')
  3. Save it in Xdata var and do similar for Ydata
  4. Now the first children of axes is the line bw two points. access two points Xdata and Ydata which are of size 2. Now interpolate all the Xdata points of children 2 (the scatter) so you can apply threshold. (we have line value for each corresponding Xdata of scatter)
  5. then apply condition ( currently) i am accessing points below line and replacing points above line with NaN
  6. Replace 2nd children's ydata with new generated ydata
for ii = 1:length(ax)
if(length(ax(ii).Children)>1)
Xdata=ax(ii).Children(2).XData;% scatter Xdata
Ydata=ax(ii).Children(2).YData;% scatter-Ydata
Thdata=interp1(ax(ii).Children(1).XData,ax(ii).Children(1).YData,Xdata);% interpolate limits
ind =Ydata>Thdata;
Ydata(ind) = NaN;
ax(ii).Children(2).YData = Ydata;
end
end

6 Comments

Dear Mehmed,
Thank you so much for your help. I tried the code as below but I see all the figure not sure what i was missing. Can you please show in an example how I can focuse on some part of the figure?
%%
% your example code
Fields = [1, 4, 5];
Fields_time = Fields +16;
MT_All = rand(100,26);
% MT_All = rand(100,9);
VariableNames={'sigma','sigma','sigma','tau','tau','tau','mNPV'}; % changed to ensure valid syntax
Mat_All_1_4_5 = [MT_All(:,Fields), MT_All(:,Fields_time), MT_All(:,end-1)];
figure
c = corrplot(Mat_All_1_4_5, 'varNames', VariableNames);
% get current figure handle
fh = gcf;
% find x and y label strings that are not empty within subplots
yLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.YLabel.String),fh.Children,'UniformOutput',false)));
xLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.XLabel.String),fh.Children,'UniformOutput',false)));
% rename y labels
for ik = 1:length(yLabelN)
if ik <= 3
fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
elseif ik <=6
fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
else
fh.Children(yLabelN(ik)).YLabel.String = sprintf('m_{NPV}');
fh.Children(xLabelN(ik)).XLabel.String = sprintf('m_{NPV}');
end
end
ax = findobj(fh,'Tag','PlotMatrixScatterAx');
for ii = 1:length(ax)
if(length(ax(ii).Children)>1)
Xdata=ax(ii).Children(2).XData;% scatter Xdata
Ydata=ax(ii).Children(2).YData;% scatter-Ydata
Thdata=interp1(ax(ii).Children(1).XData,ax(ii).Children(1).YData,Xdata);% interpolate limits
ind =Ydata>Thdata;
Ydata(ind) = NaN;
ax(ii).Children(2).YData = Ydata;
end
end
i am really sorry, i though you need the part of figure below the line.
i.e. below pink line.
Fields = [1, 4, 5];
Fields_time = Fields +16;
MT_All = rand(100,26);
% MT_All = rand(100,9);
VariableNames={'sigma','sigma','sigma','tau','tau','tau','mNPV'}; % changed to ensure valid syntax
Mat_All_1_4_5 = [MT_All(:,Fields), MT_All(:,Fields_time), MT_All(:,end-1)];
figure
[r,p,h] = corrplot(Mat_All_1_4_5, 'varNames', VariableNames);% change here pass handle output
% get current figure handle
fh = gcf;
% find x and y label strings that are not empty within subplots
yLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.YLabel.String),fh.Children,'UniformOutput',false)));
xLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.XLabel.String),fh.Children,'UniformOutput',false)));
% rename y labels
for ik = 1:length(yLabelN)
if ik <= 3
fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
elseif ik <=6
fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
else
fh.Children(yLabelN(ik)).YLabel.String = sprintf('m_{NPV}');
fh.Children(xLabelN(ik)).XLabel.String = sprintf('m_{NPV}');
end
end
hd = find(tril(true(size(h)),-1));
f = figure,
for i=1:length(hd)
copyobj(h(hd(i)).Parent,f);
end
Perfect, thanks so much..any way to show only the lowest row?
h contains objects according to there position in figure
type
h
Hi,
any idea on how to plot the Rho on these halved graphs?
thanks!

Sign in to comment.

More Answers (1)

Hi Yaser, I ran into similar issues and I was able to create a simple fix with help from Matlab tech. See an example with the 'corrplot' data in matLab:
Before (using only corrplot()):
load Data_Canada; %this is an inbuilt dataset
corrplot(DataTable)
Apply the following script:
figure;
[Cr, ~, h] = corrplot(DataTable);
% Find the index to remove the upper triangular part alone, this might
% be different for you base on you data but you can manually import the indexes
% or change the conditions in tril and find
t = tril(Cr, -1) > 0;
mirrors = find(t == 1);
% delete the unnecessary subplots
for i=1:size(mirrors)
delete(subplot(5,5,mirrors(i)));
end
Result
Hope this helps.

1 Comment

Hello Jancoba, thanks for posting this, it was quite useful!
The code you provided has a little bug when there are negative correlations in the data. Making the following modification fixes the issue.
t = abs(tril(Cr, -1)) > 0;

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!