Overlaying two axes in a Matlab plot

后端 未结 1 1725
旧时难觅i
旧时难觅i 2021-02-15 17:31

I am looking for a way to overlay an x-y time series, say created with \'plot\', on top of a display generated by \'contourf\', with different scaling on the y-axes.

It

1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-15 18:05

    I don't think there's a "clean" way to do this, but you can fake it by overlaying two axes over each other.

    x1 = 1:1:50;
    y1 = 1:1:10;
    temp_data = rand(10,50);
    y2 = rand(50,1)*20;
    figure;
    contourf(x1, y1, temp_data);
    colormap('gray');
    h_ax = gca;
    h_ax_line = axes('position', get(h_ax, 'position')); % Create a new axes in the same position as the first one, overlaid on top
    plot(x1,y2,'r-');
    set(h_ax_line, 'YAxisLocation', 'right', 'xlim', get(h_ax, 'xlim'), 'color', 'none'); % Put the new axes' y labels on the right, set the x limits the same as the original axes', and make the background transparent
    ylabel(h_ax, 'Contour y-values');
    ylabel(h_ax_line, 'Line y-values');
    

    In fact, this "plot overlay" is almost definitely what the plotyy function does internally.

    Here's example output (I increased the font size for legibility): overlaid axes

    0 讨论(0)
提交回复
热议问题