在曲线的边缘绘制箭头 [英] Plotting arrows at the edges of a curve

查看:91
本文介绍了在曲线的边缘绘制箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到此问题的启发Plot,ContourPlot等产生的曲线末端的最佳方法是什么?这些是在高中时看到的情节类型,表明曲线在页面末尾延续.

Inspired by this question at ask.sagemath, what is the best way of adding arrows to the end of curves produced by Plot, ContourPlot, etc...? These are the types of plots seen in high school, indicating the curve continues off the end of the page.

经过一些搜索,我找不到内置的方法或最新的软件包来执行此操作. (有 ArrowExtended ,但是它已经很老了.)

After some searching, I could not find a built-in way or up-to-date package to do this. (There is ArrowExtended, but it's quite old).

ask.sagemath问题中给出的解决方案取决于函数及其端点的知识,以及(也许)获取导数的能力.将其翻译成Mathematica是

The solution given in the ask.sagemath question relies on the knowledge of the function and its endpoints and (maybe) the ability to take derivatives. Its translation into Mathematica is

f[x_] := Cos[12 x^2]; xmin = -1; xmax = 1; small = .01; 
Plot[f[x],{x,xmin,xmax}, PlotLabel -> y==f[x], AxesLabel->{x,y},
  Epilog->{Blue,
    Arrow[{{xmin,f[xmin]},{xmin-small,f[xmin-small]}}],
    Arrow[{{xmax,f[xmax]},{xmax+small,f[xmax+small]}}]
  }]

另一种方法是将Plot[]生成的Line[]对象简单地替换为Arrow[].例如

An alternative method is to simply replace the Line[] objects generate by Plot[] with Arrow[]. For example

Plot[{x^2, Sin[10 x], UnitStep[x]}, {x, -1, 1}, 
  PlotStyle -> {Red, Green, {Thick, Blue}},
  (*AxesStyle -> Arrowheads[.03],*) PlotRange -> All] /. 
 Line[x__] :> Sequence[Arrowheads[{-.04, .04}], Arrow[x]]

但是这有一个问题,即行中的任何不连续点都会在您不希望出现的地方生成箭头(通常可以通过选项Exclusions -> None进行修复).更重要的是,对于CountourPlot来说,这种方法是没有希望的.例如尝试

But this has the problem that any discontinuities in the lines generate arrow heads where you don't want them (this can often be fixed by the option Exclusions -> None). More importantly, this approach is hopeless with CountourPlots. Eg try

ContourPlot[x^2 + y^3 == 1, {x, -2, 2}, {y, -2, 1}] /. 
  Line[x__] :> Sequence[Arrowheads[{-.04, .04}], Arrow[x]]

(上述情况下的问题可以通过{a___, l1_Line, l2_Line, b___} :> {a, Line[Join[l2[[1]], l1[[1]]]], b}规则或使用适当的单头箭头来解决.)

(the problems in the above case can be fixed by the rule, e.g., {a___, l1_Line, l2_Line, b___} :> {a, Line[Join[l2[[1]], l1[[1]]]], b} or by using appropriate single headed arrows.).

如您所见,以上两种(快速的技巧)都不是特别健壮或灵活的.有人知道这种方法吗?

As you can see, neither of the above (quick hacks) are particularly robust or flexible. Does anyone know an approach that is?

推荐答案

以下内容似乎可以通过首先对段进行排序来实现:

The following seems to work, by sorting the segments first:

f[x_] := {E^-x^2, Sin[10 x], Sign[x], Tan[x], UnitBox[x], 
             IntegerPart[x], Gamma[x],
             Piecewise[{{x^2, x < 0}, {x, x > 0}}], {x, x^2}}; 

arrowPlot[f_] := 
 Plot[{#}, {x, -2, 2}, Axes -> False, Frame -> True, PlotRangePadding -> .2] /.

 {Hue[qq__], a___, x___Line} :> {Hue[qq], a, SortBy[{x}, #[[1, 1, 1]] &]} /. 

 {a___,{Line[x___], d___, Line[z__]}} :> 
                           List[Arrowheads[{-.06, 0}], a, Arrow[x], {d}, 
                                             Arrowheads[{0, .06}], Arrow[z]] /. 

 {a___,{Line[x__]}}:> List[Arrowheads[{-.06, 0.06}], a, Arrow[x]] & /@ f[x];  

arrowPlot[f]

这篇关于在曲线的边缘绘制箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆