How to use hold command with Plotyy and Subplot?
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
dpb
2020 年 1 月 16 日
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
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 件のコメント
その他の回答 (1 件)
dpb
2020 年 1 月 15 日
編集済み: dpb
2020 年 1 月 15 日
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
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 件のコメント
dpb
2020 年 1 月 16 日
編集済み: dpb
2020 年 1 月 16 日
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?,
- Yes. You used two calls, no reason to not just address the columns desired by subscripting,
- 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
2020 年 1 月 16 日
"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...
参考
カテゴリ
Help Center および File Exchange で Two y-axis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!