【问题标题】:How to get arrows on axes in MATLAB plot?如何在 MATLAB 图中获取轴上的箭头?
【发布时间】:2013-06-11 15:01:30
【问题描述】:

我想画这样的东西:

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

我无法获得类似于此图中的轴:

我知道我可以像this question 中那样删除顶部和右侧的线,但我不知道如何获得边缘上的箭头。

我不需要额外的注释,但我想删除轴上的刻度。我知道当轴“正常”时如何执行此操作,但我不确定当轴已被操纵时是否必须以其他方式完成。

有人知道怎么做吗?

【问题讨论】:

  • 只是一个警告:MATLAB 不是此类工作的正确工具。这种图形是(并且应该只是)一个数量级的东西,inkscape、MS paint、GIMP 或类似的东西就足够了。尽管在 MATLAB 中可能做到这一点,但请准备好看到一些相当长且丑陋的代码,这些代码除了尝试减少 MATLAB 默认使用的更详细的绘图方法的细节外,几乎没有其他作用。

标签: matlab plot


【解决方案1】:

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

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

结果:

【讨论】:

  • 一个大大的+1,只是让我脸上露出笑容:P 我印象深刻(虽然并不惊讶,看到你的xkcd-graph)。谢谢!
【解决方案2】:

符号数学工具箱有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 属性设置为一个荒谬的小值也无济于事。

但是对于打印来说,白色的小边框不应该是相关的。

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 2011-11-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多