How set tick location in logarithmic subplot ?

Hi,
I work on oceanographic data to display the speed current and the salinity along the depth. I make a subplot in which each plot correspond to a specific hour of tide. But the problem is the tick location in logarithmic axis which change for each plot (see the plot).
I want the same scale for the salinity axis and in each plot. To make this, i have try to change the data aspect ratio but nothing change. I hope someone will try to help me. Please find my code below:
fig_02 = figure;
set(gcf,'color','white','PaperType','A4','paperorientation','landscape');
S1 = subplot(2,5,1);
% ====== 1st variable ======
L1 = line(AQD.speed(202:219),AQD.pressure(202:219),'LineStyle','none','Marker','.','color',color(4,:));
ax1 = gca;
set(ax1,'XColor',color(4,:),'YColor','k','Ydir','reverse','YTicklabel',{'0';'';'2';'';'4';'';'6';'';'8'});
xlim([0 1.5]); ylim([0 8]);
% legend(L1,'Vit. courant','Location','NorthWestOutside');
% ====== 2nd variable ======
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k','Ygrid','on');
set(ax2,'XScale','log','XTick',0:5:35,'XTickLabel',{'0','','10','','20','','30',''},'Ydir','reverse','YTicklabel','');
xlim([0 0.1]); ylim([0 8]);
L2 = line(PF_4(1:9,5),PF_4(1:9,2),'LineStyle','-.','Marker','*','MarkerSize',6,'Color','k','Parent',ax2);
% legend(L2,'Salinité','Location','SouthWestOutside');
%..........................................................................
subplot(2,5,2)
% ====== 1st variable ======
L3 = line(AQD.speed(690:701),AQD.pressure(690:701),'LineStyle','none','Marker','.','color',color(4,:));
ax1 = gca;
set(ax1,'XColor',color(4,:),'YColor','k','Ydir','reverse','YTicklabel',{'0';'';'2';'';'4';'';'6';'';'8'});
xlim([0 1.5]); ylim([0 8]);
% ====== 2nd variable ======
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k','Ygrid','on');
set(ax2,'XScale','log','XTick',0:5:35,'XTickLabel',{'0','','10','','20','','30',''},'Ydir','reverse','YTicklabel','');
xlim([0 0.1]); ylim([0 8]);
L4 = line(PF_7(1:9,5),PF_7(1:9,2),'LineStyle','-.','Marker','*','MarkerSize',6,'Color','k','Parent',ax2);
%..........................................................................
subplot(2,5,3)
% ====== 1st variable ======
L5 = line(AQD.speed(1749:1772),AQD.pressure(1749:1772),'LineStyle','none','Marker','.','color',color(4,:));
ax1 = gca;
set(ax1,'XColor',color(4,:),'YColor','k','Ydir','reverse','YTicklabel',{'0';'';'2';'';'4';'';'6';'';'8'});
xlim([0 1.5]); ylim([0 8]);
% ====== 2nd variable ======
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k','Ygrid','on');
set(ax2,'XScale','log','XTick',0:5:35,'XTickLabel',{'0','','10','','20','','30',''},'Ydir','reverse','YTicklabel','');
xlim([0 0.1]); ylim([0 8]);
L6 = line(PF_13(1:9,5),PF_13(1:9,2),'LineStyle','-.','Marker','*','MarkerSize',6,'Color','k','Parent',ax2);
%..........................................................................
subplot(2,5,4)
% ====== 1st variable ======
L7 = line(AQD.speed(2895:2919),AQD.pressure(2895:2919),'LineStyle','none','Marker','.','color',color(4,:));
ax1 = gca;
set(ax1,'XColor',color(4,:),'YColor','k','Ydir','reverse','YTicklabel',{'0';'';'2';'';'4';'';'6';'';'8'});
xlim([0 1.5]); ylim([0 8]);
% ====== 2nd variable ======
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k','Ygrid','on');
set(ax2,'XScale','log','XTick',0:5:35,'XTickLabel',{'0','','10','','20','','30',''},'Ydir','reverse','YTicklabel','');
xlim([0 0.1]); ylim([0 8]);
L8 = line(PF_19(1:9,5),PF_19(1:9,2),'LineStyle','-.','Marker','*','MarkerSize',6,'Color','k','Parent',ax2);
%..........................................................................
subplot(2,5,5)
% ====== 1st variable ======
L9 = line(AQD.speed(3750:3768),AQD.pressure(3750:3768),'LineStyle','none','Marker','.','color',color(4,:));
ax1 = gca;
set(ax1,'XColor',color(4,:),'YColor','k','Ydir','reverse','YTicklabel',{'0';'';'2';'';'4';'';'6';'';'8'});
xlim([0 1.5]); ylim([0 8]);
% ====== 2nd variable ======
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k','Ygrid','on');
set(ax2,'XScale','log','XTick',0:5:35,'XTickLabel',{'0','','10','','20','','30',''},'Ydir','reverse','YTicklabel','');
xlim([0 0.1]); ylim([0 8]);
L10 = line(PF_25(1:9,5),PF_25(1:9,2),'LineStyle','-.','Marker','*','MarkerSize',6,'Color','k','Parent',ax2);
Best regards,
Sylvain

1 件のコメント

dpb
dpb 2013 年 10 月 30 日
編集済み: dpb 2013 年 10 月 30 日
...
set(ax2,'XScale','log', ...
'XTick',0:5:35, ...
'XTickLabel',{'0','','10','','20','','30',''}, ...
...
xlim([0 0.1]);
You've tried to set a log-scale axis value to zero -- this can't be good. I'm not positive what handle graphics does with it but since log(0)-->-Inf at best it just ignores it.
Plus, you've set the axis x-limits to the range [0 0.1] after telling it to put the tick marks at 0:5:35 -- thus only one tick is in range of the x-limits. It's not clear to me why you even have any ticks showing.
What you need to do is to set ticks at the desired values within the x-axis limits based on the scale of the data. If you then want those ticks labelled as something other than their actual numeric value, those can be done w/ xticklabel on a 1:1 basis.
Instead of the full-blown code with no data for anybody to try to reproduce and play with, how about just giving a sample (short) data set or two that are giving problem in setting the scale and a description of what scale values you really want. And, what does '0' mean on a log axis???? It belongs way off on the far western horizon in reality...

サインインしてコメントする。

 採用された回答

Kelly Kearney
Kelly Kearney 2013 年 10 月 30 日

0 投票

Why are you plotting salinity on a log scale in the first place? At first I thought perhaps you were trying to get your salinity and current values on the same scale so you wouldn't have to use separate axes for them, but that doesn't seem to be the case; you've got the multiple axes set up properly. I don't think I've ever seen a salinity profile on a log scale (but then, I don't work with freshwater environments, so perhaps it's common there?).
Anyway, you could probably simplify your code with the use of a few loops. Then, as dpb pointed out, you need to set a non-zero lower limit for your axes; I recommend using the minimum of your data:
depth = linspace(0, 8, 20);
adcp = rand(20,5) * 1.5;
salt = 5 + rand(20,5) * 20;
for ii = 1:5
h.ax(ii,1) = subplot(1,5,ii);
h.ax(ii,2) = axes('position', get(h.ax(ii,1), 'position'), ...
'color', 'none', 'xaxisloc', 'top');
h.adcp(ii) = line(adcp(:,ii), depth, 'parent', h.ax(ii,1), ...
'marker', '.', 'color', 'b', 'linestyle', 'none');
h.salt(ii) = line(salt(:,ii), depth, 'parent', h.ax(ii,2), ...
'marker', '.', 'color', 'k', 'linestyle', '-');
end
set(h.ax(:,1), 'xlim', [0 1.5], 'xcolor', 'b');
tk = 5:5:30;
tklbl = strtrim(cellstr(num2str(tk')));
[tklbl{1:2:end}] = deal('');
set(h.ax(:,2), 'xscale', 'log', 'xtick', tk, 'xticklabel', tklbl, ...
'xlim', [min(salt(:)) 35]);

1 件のコメント

Sylvain
Sylvain 2013 年 11 月 4 日
A non-zero lower limit is the answer of my problem. I work on estuaries and often we have a stratification with a thin layer of fresh water in surface.
Thanks to you and dpb.

サインインしてコメントする。

その他の回答 (0 件)

製品

質問済み:

2013 年 10 月 30 日

コメント済み:

2013 年 11 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by