[Plotting] Two x and y axes for the same data, with different units... (2024)

92 views (last 30 days)

Show older comments

D. Plotnick on 14 Feb 2019

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal

Edited: Cris LaPierre on 11 Nov 2020

Accepted Answer: Cris LaPierre

Open in MATLAB Online

Hello all,

I have asked a similar question before, but have yet to find/create a satisfactory solution. I am attempting to plot position data that comes in latitude-longitude. These are over a relatively small area, so there is a direct conversion from lat/lon into meters N/E relative to some reference point. I am attempting to plot the data with the meters-E and meters-N on the x and y axes respectively. I want a second pair of xx and yy axes on the top and right side respectively plotting the latitude and longitude. This I have managed to do. The sticking point is that I want to lock the figure to equal axes in x-y direction in meters so that the data has a realistic aspect ratio. This then kills the second pair of axes. Two sample image, and then some pseudo-code below:

Here we can see what I am going for with two major problems: the y-tick labels are cut-off on the right side, and the x-y axes are squished.

[Plotting] Two x and y axes for the same data, with different units... (2)

Now, I use axis equal on the first axes (the one in meters). This actually has the correct aspect ratio, but you can see that the second axies has not remained locked with the first. This is bad.

[Plotting] Two x and y axes for the same data, with different units... (3)

This whole process is really kludgey. The closest I got was to add a pbaspect command to the second axes and I get. It is still definitely not correct. [Plotting] Two x and y axes for the same data, with different units... (4)

I am simply floored that there isn't really a way to have multiple axes for the same data. I will attach a gross code example below, and hope that someone has a suggestion. The core technique was suggested in this Mathworks page.

% Lat/lon, convert to meters

lat0 = 51.2697; % deg

lon0 = -77.3379; % deg

dLat = 1.5696e-07; %meters/radian lat here

dLon = 2.5087e-07; %meters/radian lon here

% Set up some random walk

dx = rand(100,1);

dy = rand(100,1);

x = c*msum(dx);

y = c*msum(dy);

% Convert that to lat/lon

lat = 360/(2*pi)*y*dLat + lat0;

lon = 360/(2*pi)*x*dLon + lon0;

% Plot our walk in meters

close all

figure();

plot(x,y,'b.');

xlabel('Easting (m)');

ylabel('Northing (m)');

set(gca,'box','off')

% Set axes as equal.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% comment out for other option

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

axis equal; axis tight;

% Suggested by Mathworks

ax1 = gca; % current axes

ax1_pos = ax1.Position; % position of first axes

ax2 = axes('Position',ax1_pos,...

'XAxisLocation','top',...

'YAxisLocation','right',...

'Color','none');

% Super Kludge, try to set our x/y-ticks

xt = xticks;

yt = yticks;

% Only grab 1/3 of them for spacing

xticks(xt(1:3:end));

yticks(yt(1:3:end));

xt = xticks;

yt = yticks;

% Set up some tick labels

latL = linspace(min(lat),max(lat),length(xt));

lonL = linspace(min(lon),max(lon),length(yt));

xticklabels(lonL);

yticklabels(latL);

% TRY to fix our aspect ratio issue.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% comment out for other option

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pbaspect([dLat/dLon,1,1]);

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Cris LaPierre on 14 Feb 2019

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#answer_361101

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#answer_361101

Edited: Cris LaPierre on 15 Feb 2019

Open in MATLAB Online

You are not setting the same properties on ax2 as ax1 (axis equal; axis tight). However, position of an axis does not change with 'axis tight', but plotting canvas does. This will cause the two axes to still be unaligned. If you don't do axis tight, you'd get this:

[Plotting] Two x and y axes for the same data, with different units... (6)

Here's the code I used

% Lat/lon, convert to meters

lat0 = 51.2697; % deg

lon0 = -77.3379; % deg

dLat = 1.5696e-07; %meters/radian lat here

dLon = 2.5087e-07; %meters/radian lon here

% Set up some random walk

dx = rand(100,1);

dy = rand(100,1);

x = c*msum(dx);

y = c*msum(dy);

% Convert that to lat/lon

lat = 360/(2*pi)*y*dLat + lat0;

lon = 360/(2*pi)*x*dLon + lon0;

% Plot our walk in meters

close all

figure();

scatter(x,y,'b.');

xlabel('Easting (m)');

ylabel('Northing (m)');

set(gca,'box','off')

% Set axes as equal.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% comment out for other option

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

axis equal;

% axis tight;

% Suggested by Mathworks

ax1 = gca; % current axes

ax1_pos = ax1.Position; % position of first axes

ax2 = axes('Position',ax1_pos,...

'XAxisLocation','top',...

'YAxisLocation','right',...

'Color','none');

axis equal

% axis tight

% Add labels

ax1Xtck = ax1.XTick';

ax1Ytck = ax1.YTick';

ax2.XTickLabel = num2str(interp1(x,lon,ax1Xtck,'linear','extrap'));

ax2.YTickLabel = num2str(interp1(y,lat,ax1Ytck,'linear','extrap'));

4 Comments

Show 2 older commentsHide 2 older comments

Cris LaPierre on 15 Feb 2019

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671500

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671500

Edited: Cris LaPierre on 15 Feb 2019

Open in MATLAB Online

If you really want axis tight as well, you have to put some data on ax2 so that it knows what it is tightening to. It doesn't have to be visible, but does have to be there. Try this. It's not pretty, but it works.

% Lat/lon, convert to meters

lat0 = 51.2697; % deg

lon0 = -77.3379; % deg

dLat = 1.5696e-07; %meters/radian lat here

dLon = 2.5087e-07; %meters/radian lon here

% Set up some random walk

dx = rand(100,1);

dy = rand(100,1);

x = c*msum(dx);

y = c*msum(dy);

% Convert that to lat/lon

lat = 360/(2*pi)*y*dLat + lat0;

lon = 360/(2*pi)*x*dLon + lon0;

% Plot our walk in meters

close all

figure();

scatter(x,y,'b.');

xlabel('Easting (m)');

ylabel('Northing (m)');

set(gca,'box','off')

% Set axes as equal.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% comment out for other option

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

axis equal;

axis tight;

% Suggested by Mathworks

ax1 = gca; % current axes

ax1_pos = ax1.Position; % position of first axes

ax2 = axes('Position',ax1_pos,...

'XAxisLocation','top',...

'YAxisLocation','right',...

'Color','none');

line(x([1 end]),y([1 end]),'Parent',ax2,'Linestyle','none')

axis equal

axis tight

% Add labels

ax1Xtck = ax1.XTick';

ax1Ytck = ax1.YTick';

ax2.XTickLabel = num2str(interp1(x,lon,ax1Xtck,'linear','extrap'));

ax2.YTickLabel = num2str(interp1(y,lat,ax1Ytck,'linear','extrap'));

[Plotting] Two x and y axes for the same data, with different units... (8)

Cris LaPierre on 15 Feb 2019

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671501

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671501

Edited: Cris LaPierre on 15 Feb 2019

BTW, Tick labels are strings and don't have to correspond to the actual x or y value where they are. Because the labels already align with ax1, I thought it might be easier to use the values of lon and lat you already computed, so I use interp1 to interpolate the values for ax2 using the vectors for x, y, lat, lon and the current ax1 tick values.

D. Plotnick on 15 Feb 2019

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671510

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671510

Thanks! A slightly modified version of the above work correctly, with the exception of "axis tight" still being squirelly.

I got around this by adding in 'linkaxes(ax1,ax2)', which prevents issues when I slide the view (canvas?) around, and then I can approximate 'axis tight' using 'axis([min(x) max(x) min(y) max(y)]);'

I now have some of the tick labels running into each other, but I know how to fix that.

Thanks again for your help. I do think that this would be a useful type of command to have in the core Matlab plotting library, and may submit a feature request for it. One where you can add a second set of tick-marks/units automatically locked to the view set using the initial set. Very useful for plotting things that have 2 standard units associated with them, pressure in bars vs psi for example. For some of these cases, there may be 3 or 4 standard units, but that may be asking for a bit much from the right side axis.

Cheers,

-Dan

Cris LaPierre on 15 Feb 2019

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671536

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/445057-plotting-two-x-and-y-axes-for-the-same-data-with-different-units-and-axis-equal#comment_671536

Edited: Cris LaPierre on 11 Nov 2020

This isn't the first time this request has come up. If it would be useful to you, please do submit the request.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAxis Labels

Find more on Axis Labels in Help Center and File Exchange

Tags

  • plotting
  • multiaxes
  • plotyy
  • plotxx
  • data with multiple unit representations

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


[Plotting] Two x and y axes for the same data, with different units... (12)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

[Plotting] Two x and y axes for the same data, with different units... (2024)
Top Articles
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6186

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.