创建具有两个 y 轴的 xy 图 [英] Create an xy plot with two y axis
问题描述
我有以下代码.我正在尝试创建一个带有两个 y 轴的 xy 图.但是我只在中间得到一条线.我希望 y 是右侧的垂直轴,vel 是左侧的垂直轴.我有几组不同位置的数据,我试图将第一个放在 x 轴上的 0.66,第二个放在 1 等处,但我无法让它工作.请帮忙.
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.
问候,杰尔
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);
推荐答案
一个选项是 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);
Box
属性关闭围绕每个轴绘制外部框.我建议关闭除一个轴之外的所有轴以消除轴刻度混乱.
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.
Position
属性将轴的大小和位置设置为与第一个轴完全相同.请注意,我使用了 R2014b 中引入的点符号,如果您有旧版本,只需将 h.ax1.Position
与 get(h.ax1, 'Position')
交换>.
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')
.
Color
和 YAxisLocation
调用应该是不言自明的.
The Color
and YAxisLocation
calls should be self explanatory.
我使用 hold
来保留轴格式.如果您不包括这些并绘制您的数据,它将重置背景颜色和轴位置,要求您将它们调整回来.
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.
希望这有帮助!
这篇关于创建具有两个 y 轴的 xy 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!