如何在MATLAB图中获取轴上的箭头?

时间:2013-06-11 15:01:31

标签: matlab plot

我想绘制这样的事情:

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));

我无法设法让轴与此图中的轴类似:

enter image description here

我知道我可以移除this question中的顶部和右部线条,但我不知道如何在边缘上取箭头。

我不需要额外的注释,但我想删除轴上的刻度线。我知道当轴是“正常”时如何做到这一点,但我不确定当轴已被操纵时是否必须以另一种方式完成。

有谁知道怎么做?

2 个答案:

答案 0 :(得分:14)

好吧,不要说我没有警告你:)

% 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)

结果:

enter image description here

答案 1 :(得分:5)

符号数学工具箱有provisions for making these arrows,但如果没有该工具箱,您将无法自行绘制箭头。以下代码对此有用:

% 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'))

唯一的缺点是,对于某些图形窗口大小,箭头下方会有一个1像素的白色边框,并且将轴的LineWidth属性设置为荒谬的小值无济于事。

但是对于打印,小白边应该不相关。