MATLAB: Plotting/Saving X-Y views of mesh function

2019-02-11 09:28发布

As the title says, I'm trying to save the 2-variable slices of a mesh function (as a .jpg, for example) as a subplot. I want to do this using a .m file because I have many plots to generate. I have figured out how to plot the views on their own figures, but I cannot get them to plot properly as subplots within a figure. To illustrate what I mean:

Here are the outputs on individual plots:

3D mesh: 3D MATLAB mesh plot
XY view: XY MATLAB mesh view
YZ view: YZ MATLAB mesh view
XZ view: XZ MATLAB mesh view

And here is my plotting code (not working):

%Ambiguity Surface
fid = figure(fnum);
    axes1 = axes('Parent',fid);
    view(axes1,[-62.5 28]);
    grid(axes1,'on');
    hold(axes1,'all');
    msh = mesh(taux,fdy,z,'Parent',axes1);
    xlabel ('Delay - seconds');
    ylabel ('Doppler - Hz');
    zlabel ('Ambiguity function (Normalized Magnitude-Squared)');
    fname = strcat(name,' (Ambiguity Function z(\tau;F_d))');
    title(fname);
    cb = colorbar('peer',axes1);
    set(get(cb,'ylabel'),'String','Magnitude-Squared (dB)');
    hold off;
    printFig(fid,fnum,sname)
    fnum = fnum + 1;

%Ambiguity Slices
fid = figure(fnum);
    hold all;
    subplot(2,1,1);
        axes1 = axes();
        grid(axes1,'on');
        view(axes1,[90 0]);
        msh = mesh(taux,fdy,z);
        xlabel ('Delay - seconds','Visible','off');
        ylabel ('Doppler - Hz');
        zlabel ('Ambiguity function (Normalized Magnitude-Squared)','Visible','off');
        fname = strcat(name,' (Ambiguity Function Slice z(\tau;F_d) @ \tau = 128)');
        title(fname)
    subplot(2,1,2);
        axes2 = axes();
        grid(axes2,'on');
        view(axes2,[0 0]);
        msh = mesh(taux,fdy,z);
        xlabel ('Delay - seconds','Visible','off');
        ylabel ('Doppler - Hz','Visible','off');
        zlabel ('Ambiguity function (Normalized Magnitude-Squared)','Visible','off');
        cb = colorbar('peer',axes2);
        set(get(cb,'ylabel'),'String','Magnitude-Squared');
        fname = strcat(name,' (Ambiguity Function Slice z(\tau;F_d) @ F_d = 0)');
        title(fname)
    hold off;
    printFig(fid,fnum,slname)
    fnum = fnum+1;

printFig() just sets up directory info and does print command.

My code sets up the two subplots and then overlays a full 3-d view of the mesh plot, which is not what I want. I'd like to see two of the views (XZ and YZ) on a single figure.

Thanks for the help!

-Dylan

EDIT: Per @Andrew_L's suggestion, I modified this in my code:

sp1 = subplot(2,1,1);
       axes(sp1);
       axes1 = axes();
       grid(axes1,'on');
       view(axes1,[90 0]);
       msh = mesh(taux,fdy,z,'Parent',axes1);

This is repeated for the other subplot. The result is still the same, however. It appears to set up the two blank subplots properly and then display the full pseudo-3D plot over it.

2条回答
Explosion°爆炸
2楼-- · 2019-02-11 09:44

Here is a stripped example very similar to what you are trying to achieve:

%# create axes, and set the view of each
hAx(1) = subplot(221); h = mesh(peaks);   view(3)
hAx(2) = subplot(222); copyobj(h,hAx(2)); view(0,90), title('X-Y')
hAx(3) = subplot(223); copyobj(h,hAx(3)); view(0,0) , title('X-Z')
hAx(4) = subplot(224); copyobj(h,hAx(4)); view(90,0), title('Y-Z')

%# set properties of axes
for i=1:4
    grid(hAx(i), 'on')
    axis(hAx(i), 'tight')
    xlabel(hAx(i), 'Delay (sec)');
    ylabel(hAx(i), 'Doppler (Hz)');
    zlabel(hAx(i), 'Ambiguity function');
end
title(hAx(1), 'Short Tone Ping z(\tau;F_d)')
hc = colorbar('Peer',hAx(1));
set(get(hc,'YLabel'), 'String','Magnitude-Squared (dB)')

screenshot

查看更多
甜甜的少女心
3楼-- · 2019-02-11 09:54

When you call axes1 = axes(); right below subplot(2,1,1);, you are setting axes1 to the default full-window axis when you call axes() without any arguments, which is causing the overlap. Instead, try using the handle returned by subplot to generate the axes handle. Try the following code for the second section:

%Ambiguity Slices
fid = figure(fnum);
    H1 = subplot(2,1,1);
        pos1 = get(H1, 'Position');
        set(H1,'Position',[pos1(1) pos1(2) 0.8*pos1(3) pos1(4)]); %leave space for colorbar;
        grid on;
        msh = mesh(taux,fdy,z);
        view([90 0]);
        mapping = caxis;
        xlabel ('Delay - seconds','Visible','off');
        ylabel ('Doppler - Hz');
        zlabel ('Ambiguity function (Normalized Magnitude-Squared)','Visible','off');
        fname = strcat(name,' (Ambiguity Function Slice z(\tau;F_d) @ \tau = 128)');
        title(fname)
    H2 = subplot(2,1,2);
        pos2 = get(H2, 'Position');
        set(H2,'Position',[pos2(1) pos2(2) 0.8*pos2(3) pos2(4)]); %leave space for colorbar;
        grid on;
        msh = mesh(taux,fdy,z);
        caxis(mapping);
        view([0 0]);
        xlabel ('Delay - seconds','Visible','off');
        ylabel ('Doppler - Hz','Visible','off');
        zlabel ('Ambiguity function (Normalized Magnitude-Squared)','Visible','off');
    axes('Position', [0.05 0.05 0.9 0.9], 'Visible', 'off'); %setup axes for colorbar;
    caxis(mapping);
    cb = colorbar();
    ylabel(cb, 'Magnitude-Squared');
    fname = strcat(name,' (Ambiguity Function Slice z(\tau;F_d) @ F_d = 0)');
    title(fname)
    printFig(fid,fnum,slname)
    fnum = fnum+1;

This (should) at least get everything displayed how you want - I believe that the colorbar scale will not correspond to anything in particular (most likely the number of discrete colors in the bar), so extra code is needed to make sure both plots use the same colormap, and that you change the colormap for the colorbar.

查看更多
登录 后发表回答