Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

matlab - Create an xy plot with two y axis

I have the following code. I'm trying to create an xy plot wth two y axis. However I only get one line down the middle. I want y to be the vertical axis to the right and vel to be the vertical axis to the left. I have several sets of data for different positions and I'm trying to place the first at 0.66 on the x-axis, the second at 1 etc but I cannot get it to work. Help please.

Regards, Jer

clc
    clear


%Retrieve data and figure setup
filename = 'G:Protable Hard DrivePHD Hard DriveExperimental DataBulkrename TrialCommon data for line graphsData for line graphs.xls';
a = xlsread(filename, 'Veldef');
vel = 0:1/37:1;
y = -16/15:1/15:21/15;

%X/D=0.66 TSR5
x = 0.66;
exp = a(1:38,2);
ko = a(1:38,4);
rst = a(1:38,6);

%Plot
h = plot(x,exp,x,ko,x,rst);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option is plotyy()

x = 1:10;
y = rand(1,10);
plotyy(x, x, x, y)

A more flexible option is to overlay two (or more) axes and specify what data you want plotted on each.

% Sample data
x = 1:10;
y = rand(1,10);

% Create axes & store handles
h.myfig = figure;
h.ax1 = axes('Parent', h.myfig, 'Box', 'off');
h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right');

% Preserve axes formatting
hold(h.ax1, 'on');
hold(h.ax2, 'on');

% Plot data
plot(h.ax1, x, x);
plot(h.ax2, x, y);

The Box property turns off drawing of the external box around each axis. I'd recommend turning it off for all but one of the axes to eliminate axis tick clutter.

The Position property sets the axis size and position to the exact same as the first axis. Note that I've used the dot notation introduced in R2014b, if you have an older version just swap h.ax1.Position with get(h.ax1, 'Position').

The Color and YAxisLocation calls should be self explanatory.

I used hold to preserve the axes formatting. If you don't include these and plot your data it will reset the background color and axes locations, requiring you to adjust them back.

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...