MATLAB Answers

How to use hold command with Plotyy and Subplot?

6 views (last 30 days)
SP
SP on 15 Jan 2020
Commented: SP on 18 Jan 2020
Dear Matlab Users,
I am facing a problem on applying hold command along with both Plotyy and subplot. Here, I am providng my code and attached my sample data for your convenience. Here, the subplot consists of 4 plotyy figures with 2*2 grid, and each plotyy fig. contains 4 plots with 2 at the left Y-axis, 1 at the right Y-axis and one hline plot for the threshold value. For adding 2 plots at the left Y-axis, I am using hold (ax1(1)), hold(ax2(1)), hold(ax3(1)), and hold(ax4(1)) in the subplot 1, 2, 3, and 4 respectively. When, I am running the attached code, it is showing an error on handle axis.
Please help me to know where I am going wrong in the attached code.
Please find the attached sample data and code matfile.
Thank you for your time.

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 16 Jan 2020
Ver 2
[y,t]=xlsread('Sample Data.xlsx');
t=datetime(t(2:end,1),'inputformat','MM/dd/uuuu','format','MMM yy');
mos=(year(t(end))-year(t(1)))*12+1+6*(month(t(end))>6);
ttk=datetime(year(t(1)),1:6:mos,1,'format','MMM yy');
for i=1:4
hAx(i)=subplot(2,2,i);
hL{i,1}=plot(t,y(:,1:2),'linewidth',1.5);
hL{i,1}=[hL{i,1}; ...
yline(0.15,'m','GHE = 0.5', ...
'linewidth',2.5, ...
'labelHorizontalAlign','center')];
xticks(ttk)
hAx(i).XTickLabelRotation=90;
hAx(i).XAxis.FontSize=9;
yyaxis right
set(gca,'YColor','r')
hL{i,2}=plot(t,y(:,i+2),'r');
end
return
results in
untitled.jpg
Really too many ticks for the space with subplot() unless you're going to make the display full screen, but it's what asked for...

  5 Comments

Show 2 older comments
SP
SP on 18 Jan 2020
Thank you very much for your valuable general suggestions based on which I will definitely learn and apply in my full-sample analysis. But, I didn't understand one of your suggestion in terms of the arrangment of xticks "Probably the most user-friendly way would be to pick one of the even divisors of 12 and compute that number as 1-, 2-, 3-,... ticks/year. ", here 1-,2-,3- stands for?
Thank you very much for your help.
dpb
dpb on 18 Jan 2020
Simply shorthand to not write 1-tick/year, 2-ticks/year, 3-ticks/year, ...
SP
SP on 18 Jan 2020
Okay, I understood. Thank you very much for your help and suggestions.

Sign in to comment.

More Answers (1)

dpb
dpb on 15 Jan 2020
Edited: dpb on 15 Jan 2020
Using the features TMW has given us more recently...in particular, datetime and yyaxis along with using some data organization by indexing will help immensely to simplify...the following code generated
untitled.jpg
I didn't bother to finish the rest of the annotation, etc., that should be simple enough to insert into the loop with some preliminaries to save the needed info for labels, etc.
The line handles and basic axis handles are all saved so can also make modifications outside the loop if necessary/desired.
[y,t]=xlsread('Sample Data.xlsx');
t=datetime(t(2:end,1),'inputformat','MM/dd/uuuu');
for i=1:4
hAx(i)=subplot(2,2,i);
hL{i,1}=plot(t,y(:,1:2));
yyaxis right
set(gca,'YColor','r')
hL{i,2}=plot(t,y(:,i+2),'r');
end

  3 Comments

SP
SP on 16 Jan 2020
Thank you very much for your response. This is partially right what I really expected. But, my each plotyy figure in the subplot should consists of 4 lines, but here, each fig. contains 3 lines, that means the hline which I have defined in my previous code to identify the threshold value of 0.5 isn't there. Please suggest to me how can I add hline code in your suggested code?
Further, I have two following queries on your suggested code
  1. In "hL{i,1}=plot(t,y(:,1:2));", what 1:2 indicates? I think you have called 1st and 2nd column in the left Y-axis, right?
  2. in "hL{i,2}=plot(t,y(:,i+2),'r')" what "i+2" indicates although I understand this is for right Y-axis?
Please suggest to me for which I shall be able to proceed with your suggestion.
Thank you for your time.
dpb
dpb on 16 Jan 2020
I just outlined the general form; finish up the refinements as desired.
Add your hline call to whichever axis you desire it to reside on--if left, before the yyaxis call, if right, after.
There is no base hline function that matches your calling syntax so can't replicate what that function does, precisely, anyways...
>> which -all hline
C:\ML_R2017\toolbox\stats\stats\private\hline.m % Private to stats
>>
Only a private function; let's see what it expects for inputs, anyways...
>> help private/hline
hline draws horizontal lines
hline(X) draws one horizontal line for each element in vector x
hline(AX,X) draws the lines to the axes specified in AX
hline(X,...) accepts HG param/value pairs for line objects
...
So, the usage you have doesn't match up with the expected inputs of just a numeric value or array of such so not same function...
For the Q?,
  1. Yes. You used two calls, no reason to not just address the columns desired by subscripting,
  2. Yes, again. The RH axis column is 3 thru 6; or index plus two of the index number for the four subplots. That's just a result of the particular order in the y array of the data and what you chose to put on each axis/plot. Could have been any other; just turned out in this case to be quite a simple way to get the ones wanted; sometimes it's not so easy so may have to resort to look up tables or somesuch.
Read up on matrix/array addressing in the Getting Started section on efficient use of MATLAB arrays and vector subscripting.
dpb
dpb on 16 Jan 2020
"There is no base hline function that matches your calling syntax so can't replicate what that function does, precisely, anyways..."
Oh. I see R2018 release introduced yline which would work. I was using R2017b and didn't have later release installed at the time. Nevertheless, hline appears in error or a local function...

Sign in to comment.

Sign in to answer this question.


Translated by