【问题标题】:Create an xy plot with two y axis创建一个带有两个 y 轴的 xy 图
【发布时间】:2015-02-20 17:07:35
【问题描述】:

我有以下代码。我正在尝试使用两个 y 轴创建一个 xy 图。但是我只在中间得到一条线。我希望 y 是右侧的垂直轴,而 vel 是左侧的垂直轴。我有几组不同位置的数据,我试图将第一组放在 x 轴上的 0.66,第二组放在 1 等,但我无法让它工作。请帮忙。

问候, 耶

clc
    clear


%Retrieve data and figure setup
filename = 'G:\Protable Hard Drive\PHD Hard Drive\Experimental Data\Bulkrename Trial\Common data for line graphs\Data 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);

【问题讨论】:

标签: matlab plot


【解决方案1】:

一个选项是plotyy()

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

更灵活的选择是叠加两个(或更多)轴并指定要在每个轴上绘制的数据。

% 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 属性关闭围绕每个轴绘制外部框。我建议对除一个轴之外的所有轴都关闭它,以消除轴刻度混乱。

Position 属性将轴大小和位置设置为与第一个轴完全相同。请注意,我使用了 R2014b 中引入的点表示法,如果您有旧版本,只需将 h.ax1.Position 替换为 get(h.ax1, 'Position')。

Color 和 YAxisLocation 调用应该是不言自明的。

我使用hold 来保留坐标轴格式。如果您不包括这些并绘制您的数据,它将重置背景颜色和坐标轴位置,需要您重新调整它们。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    从 Matlab 2016a 开始,您还可以使用新功能 yyaxis。来自documentation的示例:

    x = linspace(0,10);
    y = sin(3*x);
    yyaxis left
    plot(x,y)
    
    z = sin(3*x).*exp(0.5*x);
    yyaxis right
    plot(x,z)
    ylim([-150 150])
    

    Resulting image

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多