如何在MATLAB图中的轴上添加箭头?

10

我想绘制类似这样的图:

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

我知道可以像这个问题中那样删除顶部和右侧的线条,但我不知道如何在边缘上加箭头。

我不需要额外的注释,但我想要删除坐标轴上的刻度线。我知道当坐标轴是“正常”的时候该如何操作,但我不确定当坐标轴已经被操作过后是否需要用其他方法来完成它。

有人知道怎么做吗?


4
警告:MATLAB 不是这种工作的正确工具。这种图形只应该是一个数量级的东西,并且应该只使用 inkscape、MS paint、GIMP 或类似工具就足够了。虽然在 MATLAB 中也有可能完成此项工作,但要准备看到一些非常冗长且难看的代码,这些代码除了试图减少 MATLAB 默认情况下使用的更详细的绘图方法的细节外,几乎没有其他用处。 - Rody Oldenhuis
2个回答

20

好的,别说我没警告你 :)

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

结果:

在此输入图像描述


2
一个大大的加一,这让我笑了:P 我很印象深刻(虽然不惊讶,因为我看过你的xkcd图)。谢谢! - Stewie Griffin

6
符号数学工具箱有提供制作这些箭头的功能,但如果没有该工具箱,则需要自己绘制箭头。以下代码应该对此有帮助:
% 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属性设置为荒谬的小值也无济于事。
但是对于打印来说,这个小白边并不重要。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接