Two Y axes not lining up (semilogx for one, line for the other)

3 views (last 30 days)
Hi there,
I have two sets of axes set up. On the first (left Y and bottom X) I am plotting a "semilogx" function to plot. On the second set of axes (right Y and top X) I am simply using a "line" function to plot. The tick marks are set up the same on both (except that I manually changed the labels for the right Y axis) - they should be at 800, 600, 400, 200. The two Y axis tick marks line up fine when I am using a simple "line" function for the first set of axes. BUT when I use a "semilogx" function, for some reason the right Y axis gets these odd misalignments. Here is a sample:
Any help would be appreciated!
Thank you!
Aaron
  1 Comment
Aaron Burdick
Aaron Burdick on 29 Sep 2011
Here's the code I'm using:
hl1 = semilogx(basinput,bas_hpa,'Color',[0 0 0],'linewidth',2);
ax1 = gca;
set(ax1,'XColor',[0 0 0],'YColor',[0 0 0],'Parent',figure1,'YTickLabel',{'200','400','600','800','1000'},...
'YTick',[200 400 600 800 1000],...
'YMinorTick','on',...
'YDir','reverse',...
'XMinorTick','on',...
'Position',[0.13 0.112403846153846 0.710158478605388 0.793461538461539],...
'FontSize',16,...
'FontName','Times');
axis tight;
THEN LATER FOR THE LINE GRAPH:
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'YTick',[200 400 600 800 1000],...
'YTickLabel',{'11.8','7.18','4.20','1.95',''},...
'YMinorTick','off',...
'XMinorTick','on',...
'YDir','reverse',...
'FontSize',16,...
'FontName','Times',...
'Position',[0.13 0.112403846153846 0.710158478605388 0.793461538461539],...
'Color','none',...
'XColor','k','YColor','k');
hl2 = line(difpercent,dif_hpa,'Parent',ax2,'Color',[0 0 0],...
'linewidth',2,'linestyle',':');

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2011
For this kind of plotting, one would normally use plotyy(), specifying the plotting functions to use:
plotyy(x1,y1,x2,y2,@semilogy,@plot)
  3 Comments
Walter Roberson
Walter Roberson on 29 Sep 2011
Yes: you can set the XAxisLocation property for one of the axes to 'top', and you can set its XTick and XTickLabel at will.
Aaron Burdick
Aaron Burdick on 9 Oct 2011
Hi Walter. Thanks for your help on this. I switched to plotyy function and reprogrammed based on this. But I am still getting the same misalignment in Y axes as I was getting before. Here is the code:
% Create figure
figure1 = figure('XVisual','','Color',[1 1 1]);
% Create difpercent vector
difpercent = (difinput./basinput).*100;
% plotyy graph
[AX,H1,H2] = plotyy(basinput,bas_hpa,difpercent,dif_hpa,@semilogx,@plot)
set(AX(1),'XColor',[0 0 0],'YColor',[0 0 0],'Parent',figure1,...
'YTickLabel',{'200','400','600','800','1000'},...
'YTick',[200 400 600 800 1000],...
'YMinorTick','on',...
'YDir','reverse',...
'XMinorTick','on',...
'Position',[0.13 0.112403846153846 0.710158478605388 0.793461538461539],...
'FontSize',16,...
'FontName','Times');
set(H1,'Color',[0 0 0],'linewidth',2)
axis tight;
legend([,units,],'%','Location','Best')
% Create ylabel
ylabel('Pressure (hPa)','FontWeight','bold','FontSize',18,...
'FontName','Times');
% Create xlabel
xlabel([,xaxistitle,],'FontWeight','bold','FontSize',18,...
'FontName','Times');
set(AX(2),'XAxisLocation','top',...
'YAxisLocation','right',...
'YTick',[200 400 600 800 1000],...
'YTickLabel',{'11.8','7.18','4.20','1.95',''},...
'YMinorTick','off',...
'XMinorTick','on',...
'YDir','reverse',...
'FontSize',16,...
'FontName','Times',...
'Position',[0.13 0.112403846153846 0.710158478605388 0.793461538461539],...
'Color','none',...
'XColor','k','YColor','k');
set(get(AX(2),'Ylabel'),'String','Altitude (km)','FontWeight','bold',...
'FontSize',18,...
'FontName','Times');
set(get(AX(2),'Xlabel'),'String',[,x2axistitle,],'FontWeight','bold',...
'FontSize',18,...
'FontName','Times');
set(H2,'Color',[0 0 0],'linewidth',2,'linestyle',':')
% Set output size
set(gcf, 'units', 'pixels');
set(gcf, 'position', [200 150 650 550]);
Any idea why the Y axes would not be lining up? They are the same values, just labeled differently for the right Y axes (matching up km to pressure).
Thank you!
Aaron

Sign in to comment.

Categories

Find more on Line Plots 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!