Is it possible to plot the data and show two different scales for the same data using MATLAB 7.9 (R2009b)?

31 views (last 30 days)
I want to plot some data and show two different scales for the Y-axis. For example, the data I have plotted is in inches (for Y-axis, displayed on the left side) and I wish to show the equivalent scale in centimeters on the right side of the axis.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Mar 2010
It is possible to create a new scale that may be corresponding to the scale on the left side (Y-axis) and display it on the right side of the axis. Please follow the example below which demonstrates one way of how this may be done.
% Create data for demonstration
x = 0:0.01:20;
y = 200*exp(-0.05*x).*sin(x); % Assuming result is in inches
% Plot the results
f = figure;
a1 = axes;
plot(x,y)
% Query the YTicks
yt = get(a1,'YTick');
% Create a new axis based on the existing axis
a2 = copyobj(a1,f);
% Make the new axis transparent
set(a2,'Color','none')
% Remove the XTicks (since they are redundant)
set(a2,'Xtick',[])
% Display the YTicks on the right side
set(a2,'YAxisLocation','right')
% Convert the YTick values based on a different scale
% In this case we are converting inches into centimeters
converted_values = 2.54*yt;
% Use essentially the same ticks but update the labels
set(a2,'YTickLabel',converted_values)
% Label your plot appropriately
xlabel(a1,'X [units]')
ylabel(a1,'Y [inches]')
ylabel(a2,'Y [cms]')

More Answers (0)

Products


Release

R2009b

Community Treasure Hunt

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

Start Hunting!