【问题标题】:Plot second y axis using plot and fill (without plotyy)使用绘图和填充绘制第二个 y 轴(没有 plotyy)
【发布时间】:2014-08-21 22:47:39
【问题描述】:

这是我的代码

clear all;clc
x = linspace(0, 10, 100);

axes('Position', [.075,.075,.9,.2], ...
     'XColor',   'k', ...
     'YColor',   [0 0 0]);
fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None');
ylabel('Fancy Sine', 'FontSize', 11);

% Adding this the upper vanishes and the y axes is on left not right side
axes('Position',      [.075,.075,.9,.2], ...
     'XColor',        'k', ...
     'YAxisLocation', 'Right', ...
     'Color',         'none');
plot(x, x, 'Color', [.2 .4 .8]);
ylabel('Line Graph', 'FontSize', 11);

xlabel('X', 'FontSize', 11);

单轴工作正常

但是当我想添加第二个轴时,第一个消失了,新轴不在右边,而是在左边..

但两个图都应该在一个轴上,第二个 y 轴应该在右侧。

如何做到这一点?

【问题讨论】:

    标签: matlab plot multiple-axes


    【解决方案1】:

    plot 自动将轴属性设置为默认值。使用hold 停止此操作或在您的plot 调用之后指定轴属性。

    前者的一个例子:

    clear all;clc
    x = linspace(0, 10, 100);
    
    ax1 = axes('Position', [.075,.075,.9,.2], ...
         'XColor',   'k', ...
         'YColor',   [0 0 0]);
    p1 = fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None','Parent',ax1);
    ylabel('Fancy Sine', 'FontSize', 11);
    
    % Adding this the upper vanishes and the y axes is on left not right side
    ax2 = axes('Position',      [.075,.075,.9,.2], ...
         'XColor',        'k', ...
         'YAxisLocation', 'Right', ...
         'Color',         'none');
    hold on
    p2 = plot(ax2,x, x, 'Color', [.2 .4 .8]);
    hold off
    ylabel('Line Graph', 'FontSize', 11);
    xlabel('X', 'FontSize', 11);
    

    Edit1:您不需要为您的 fill 调用执行此操作的原因是因为它在您的坐标区中创建了一个 patch 对象并且不调用 plot 命令。

    【讨论】:

    • 领先我 4 秒。 :)
    • 有什么方法可以完全隐藏第二个 x 轴? XColor - none 不起作用..
    • @embert set(ax2,'Xtick',[]) 并在您的第二个 axes 呼叫之前转移 xlabel 呼叫,以便看起来更好。
    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多