How to get arrows on axes in MATLAB plot?

前端 未结 2 1567
醉梦人生
醉梦人生 2021-02-02 02:54

I want to plot something like this:

x = 0:0.01:10;
f = @(x) 50* 1.6.^(-x-5);
g = @(x) 50* 1.6.^(+x-10);
plot(x, f(x));
hold on
plot(x, g(x));

I

相关标签:
2条回答
  • 2021-02-02 03:11

    The symbolic math toolbox has provisions for making these arrows, but without that toolbox you are stuck with drawing the arrows yourself. The following code should be useful for this purpose:

    % determine position of the axes
    axp = get(gca,'Position');
    
    % determine startpoint and endpoint for the arrows 
    xs=axp(1);
    xe=axp(1)+axp(3)+0.04;
    ys=axp(2);
    ye=axp(2)+axp(4)+0.05;
    
    % make the arrows
    annotation('arrow', [xs xe],[ys ys]);
    annotation('arrow', [xs xs],[ys ye]);
    
    % remove old box and axes
    box off
    set(gca,'YTick',[])
    set(gca,'XTick',[])
    set(gca,'YColor',get(gca,'Color'))
    set(gca,'XColor',get(gca,'Color'))
    

    The only drawback is that for some figure window sizes you will have a 1-pixel white border below the arrows, and setting the LineWidth property of the axes to a ridiculous small value does not help.

    But for printing, the small white border should not be relevant.

    0 讨论(0)
  • 2021-02-02 03:18

    Well, don't say I didn't warn you :)

    % Some bogus functions
    f = @(x) 50* 1.6.^(-x-5);
    g = @(x) 50* 1.6.^(+x-10);
    
    % Point where they meet
    xE = 2.5;
    yE = f(xE);
    
    % Plot the bogus functions
    figure(1), clf, hold on
    x = 0:0.2:5;
    plot(x,f(x),'r',  x,g(x),'b', 'linewidth', 2)
    
    % get rid of standard axes decorations
    set(gca, 'Xtick', [], 'Ytick', [], 'box', 'off')
    
    % Fix the axes sizes
    axis([0 5 0 5])
    
    % the equilibrium point
    plot(xE, yE, 'k.', 'markersize', 20)
    
    % the dashed lines
    line([xE 0; xE xE], [0 yE; yE yE], 'linestyle', '--', 'color', 'k')
    
    % the arrows
    xO = 0.2;  
    yO = 0.1;
    patch(...
        [5-xO -yO; 5-xO +yO; 5.0 0.0], ...
        [yO 5-xO; -yO 5-xO; 0 5], 'k', 'clipping', 'off')
    
    % the squishy wiggly line pointing to the "equilibrium" text
    h = @(x)0.5*(x+0.2) + 0.1*sin((x+0.2)*14);
    x = 2.7:0.01:3.5;
    plot(x, h(x), 'k', 'linewidth', 2)
    
    % the static texts
    text(xE-yO, -0.2, 'Q^*', 'fontweight', 'bold')
    text(-2*yO,   yE, 'P^*', 'fontweight', 'bold')
    text(-2*yO,    4, 'Price', 'rotation', 90, 'fontsize', 14)
    text(    4, -0.2, 'Quantity', 'fontsize', 14)
    text(   .5,  4.2, 'Demand', 'fontsize', 14, 'rotation', -55)
    text(   4.0,  3.3, 'Supply', 'fontsize', 14, 'rotation', +55)
    text(   3.6,  2.1, 'Equilibrium', 'fontsize', 14)
    

    Result:

    enter image description here

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