how to plot semilogy on two axes
3 ビュー (過去 30 日間)
古いコメントを表示
Dear All,
I have three sets of data (y1,2,3), and I want to plot them on a semilogarithmic (y) scale using the same x scale, but I want to use different ticks and tick labels for y1 and y2 (bottom) and y3 (top)x axes, generalizing it would be two x scales. I tried plotxx but linespec for line vs semilogy are not the same, so I cannot adapt plotxx for my case.
Based on prior posts, I tried this:
% import data into matlab
y1 = zeros(14,1);
y2 = zeros(10,1);
y3 = zeros(14,1);
x = 1:14;
% paste data in y1 (miss by distance), y2 (miss by indiv amplitude), y3 (reg),
% first, plot individual y data using editor to get an idea about ylim
% code break
set (0, 'DefaultAxesFontSize', 8,'DefaultAxesFontName', 'Times');
figure;
set (gcf, 'Units', 'centimeter');
pos = get (gcf, 'Position');
pos(3) = 10;
pos(4) = 8;
set (gcf, 'Position', pos);
set (gca, 'Units', 'centimeter');
set (gca, 'Position', [1.5 1.2 8 5]);)
h1 = semilogy(x,y1,'.r','MarkerSize',6);
hold on;
h2 = semilogy(x(1:10),y2,'xb','MarkerFaceColor','b','MarkerSize',8);
hold on;
h3 = semilogy(x,y3,'ok','MarkerSize',5);
hold off;
H1 = gca;
set(H1,'Xlim',[0.5 14.5]);
set(H1,'Ylim',[10^(-2) 10^3]);
set(H1,'XTick',1:14);
set(H1,'XTickLabel',{'0','5','10','15','20','25','30','35','40','45','50','55','60','65'});
box('on');
set(H1,'tickdir','out');
ax=axis;
H2=axes('position', get(H1,'position'));
axis(ax);
set(H2,'XAxisLocation','top')
set(H2,'XTick',1:14);
set(H2,'XTickLabel',{'reg1cm_1','reg1cm_2','reg1cm_3','reg1cm_4','reg1.5cm_1','reg1.5cm_2','reg1.5cm_3','reg1.5cm_4','reg1.5cm_5','reg1.5cm_6','reg1.5cm_7','reg1.5cm_8','reg1.5cm_9'});
set(H2,'YAxisLocation','right');
set(H2,'yticklabel','');
The top tick labels are not displayed using the above, please advise.
Many thanks,
Octavian
1 件のコメント
dpb
2013 年 12 月 5 日
編集済み: dpb
2013 年 12 月 5 日
Truncate the example to the barest number of lines that shows the problem and include sample data as well so it's trivial for a reader to recreate the issue locally. Don't expect donated time/help to finish the job of asking the question for you.
W/o extensively reading the code to try to discern a problem, the first and most likely reason for customized tick lablels to not show up on an axis is that the tick values aren't in the range of the appropriate x-axis limits for the axes object in question.
採用された回答
dpb
2013 年 12 月 5 日
As surmised above, you never set the x-axis limits on the second axes...
...
H2=axes('position', get(H1,'position'));
set(H2,'XAxisLocation','top')
% Following line is missing...so the default limits are [0 1]
set(H2,'Xlim',[0.5 14.5]);
...
2 件のコメント
dpb
2013 年 12 月 5 日
I've got meeting this evening so no time to delve deeply but a quick look-see makes me wonder what you're trying to do with
set(H2,'Color','none');
That, I'd guess, makes H2 disappear. There may be something else in the ranges, etc., that's a problem, too, but pretty sure you don't want that line in there...
その他の回答 (1 件)
Octavian
2013 年 12 月 6 日
1 件のコメント
dpb
2013 年 12 月 6 日
Good on ya' for finding it...I had a meeting last night so had no opportunity to look any further. I realized later what the 'none' was for, indeed; it was just a gut reaction when glancing thru the code first pass.
I didn't try but another way you might look at that might save some machinations would be to use plotyy as a starting point then just change the locations of the RH x-axis. If, as in your case, you actually want all the data on the LH axis, simply plot a couple of NaN vectors/points on the RH if it is unhappy w/ an empty argument list for the second axes.
I think there's a plotxx or similarly named submission on the File Exchange as well you might look for; as a general rule when you have some specialized graphing need like this it's worth looking at FEX as somebody else has likely had the need before, too.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!