Clear Filters
Clear Filters

two correlations on one graph using hold on and corrplot?

4 views (last 30 days)
Hello,
I am trying to create a graph showing two linear correlations. Based on what I have found so far, I think my code should look like this:
corrplot(y(:,[1 2]),'varnames'{'var1 var2'}) %generate graph of 1st correlation
hold on
corrplot(y(:,[1 2]),'varnames'{'var1 var2'}) %generate graph of 2nd correlation and overlap it onto the %first graph
but when I run this I get two separate graphs.
What am I doing wrong?
Thank you very much!

Answers (1)

Nachiket Katakkar
Nachiket Katakkar on 23 Feb 2017
The "hold on" command applies to an axes object and not to the figure. The "corrplot" function creates a new figure, with several axes objects, each time it is called and therefore the "hold on" command does not apply here.
I am not entirely sure about why you wish to overlay the two outputs, but one workaround would be to access the each of the axes objects and copy the axes children as shown here:
%%Create the two corrplots and obtain figure handles
% This section will create 2 different figures, one for each corrplot
load Data_Canada  
% First call to corrplot
[R,PValue] = corrplot(DataTable,'tail','right');
f1 = gcf;
% Second call to corrplot
corrplot(DataTable(:,1:2),'type','Kendall','testR','on');
f2 = gcf;
%%Copy plots from one axes to another
% Obtain axes handles
ax1 = f1.Children(1);
ax2 = f2.Children(1);
% Copy first axes' children(plot objects) to second axes
copyobj(ax1.Children,ax2)
You might want to run each section individually to notice the difference. Plots from one of the first figure's axes have been copied over to the second figure's top-right axes.

Categories

Find more on Specifying Target for Graphics Output 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!