Inversely proportional secondary axis as a reference of primary axis

3 views (last 30 days)
Hello everyone;
first time here, I will appreciate any feedback and help that I can get. I am plotting in the freq. domain, but I want add a second axis in wavelength, which is (wavelength=3e8/freq), inversely proportional. The code below is my attempt to add the second axis, I am only getting the limits right but the values in between do not correspond to each other. Thank you for your time
[AX,H1,H2] = plotyy(x1,y1,x1,y2);
ax1 = gca;
delta = 0.1;
set(ax1, 'Position', get(ax1, 'Position') + [0 delta 0 -delta ]);
ax2=axes('Position', get(ax1, 'Position').* [1 1 1 0.001]-[0 delat 0 0],'Color','none');
domain=get(ax1, 'XLim')
domain=fliplr(domain).^-1
set(ax2,'xdir','reverse','Xlim',domain);

Accepted Answer

Sreeja Banerjee
Sreeja Banerjee on 9 Jun 2015
Hi Owen,
From your question I understand that the number of ticks shown on the first axis is different than the number of ticks in the new axis that you have added even though the limits are as you would like them.
This is happening because based on the limits of the second axis, the plot is automatically selecting the tick labels. You can work around this issue by modifying the code a little bit. I have shown below how you can do that:
For the first axis and the second axis to have the same number of ticks, we need to specify the 'Xtick' parameter for one of the axis. We can do that by finding out how many ticks are there in the first axis. The steps are:
a) Find the Xticks for the first axis b) Calculate the length c) Create new array of Xticks for the second axis based on the range and number of ticks d) Set Xticks for the second axis
For simplicity, I am assuming the following data for x1, y1 and y2:
x1 = 1:0.01:2*pi;
y1 = cos(x1);
y2 = sin(x1);
The changes to the code are shown below (please look carefully at the portion marked with %new code):
[AX,H1,H2] = plotyy(x1,y1,x1,y2);
ax1 = gca;
delta = 0.1;
set(ax1, 'Position', get(ax1, 'Position') + [0 delta 0 -delta]);
ax2=axes('Position', get(ax1, 'Position').* [1 1 1 0.001]-[0 delta 0 0],'Color','none');
domain=get(ax1, 'XLim')
domain=fliplr(domain).^-1
% new code begin
val=get(ax1, 'XTick')
l = length(val)
ticks = linspace(domain(1),domain(2),l)
set(ax2,'Xtick',ticks);
% new code end
set(ax2,'xdir','reverse','Xlim',domain);
I get the following output figure:

More Answers (0)

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!