Wolfram mathematica 在曲线边缘打印箭头
受ask.sagemath启发,在Wolfram mathematica 在曲线边缘打印箭头,wolfram-mathematica,plot,Wolfram Mathematica,Plot,受ask.sagemath启发,在绘图、轮廓绘图等生成的曲线末端添加箭头的最佳方法是什么。。。?这些是高中时看到的情节类型,表明曲线在页面末尾继续 经过一些搜索,我找不到一个内置的方式或最新的包来做这件事。(有,但已经很旧了) ask.sagemath问题中给出的解决方案依赖于函数及其端点的知识,以及(可能)求导数的能力。将其翻译成Mathematica是 f[x_] := Cos[12 x^2]; xmin = -1; xmax = 1; small = .01; Plot[f[x],{x,
绘图
、轮廓绘图
等生成的曲线末端添加箭头的最佳方法是什么。。。?这些是高中时看到的情节类型,表明曲线在页面末尾继续
经过一些搜索,我找不到一个内置的方式或最新的包来做这件事。(有,但已经很旧了)
ask.sagemath问题中给出的解决方案依赖于函数及其端点的知识,以及(可能)求导数的能力。将其翻译成Mathematica是
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[{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]]
但这有一个问题,即行中的任何不连续都会在您不需要的地方生成箭头(这通常可以通过选项排除->无来修复)。更重要的是,这种方法对于CountourPlot
s是没有希望的。尝试
ContourPlot[x^2 + y^3 == 1, {x, -2, 2}, {y, -2, 1}] /.
Line[x__] :> Sequence[Arrowheads[{-.04, .04}], Arrow[x]]
(上述情况下的问题可以通过规则来解决,例如,{a_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
如您所见,上述两种(快速破解)都不是特别健壮或灵活的。有人知道一种方法吗?通过首先对段进行排序,以下方法似乎有效:
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]
f[x]:={E^-x^2,Sin[10x],Sign[x],Tan[x],UnitBox[x],
整数部分[x],伽马[x],
分段[{x^2,x<0},{x,x>0}}],{x,x^2};
箭头图[f_3;]:=
Plot[{#},{x,-2,2},Axes->False,Frame->True,PlotRangePadding->.2]/。
{Hue[qq][uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
{a_u______;行[x_____;],d___;,行[z__;]}:>
列表[箭头[{-.06,0}],a,箭头[x],{d},
箭头[{0,06}],箭头[z]]/。
{a_____;,{Line[x__;]}:>列表[箭头[{-.06,0.06}],a,箭头[x]&/@f[x];
箭头图[f]
受亚历克赛的评论和贝里萨利斯的回答的启发,以下是我的尝试
makeArrowPlot[g_Graphics, ah_: 0.06, dx_: 1*^-6, dy_: 1*^-6] :=
Module[{pr = PlotRange /. Options[g, PlotRange], gg, lhs, rhs},
gg = g /. GraphicsComplex -> (Normal[GraphicsComplex[##]] &);
lhs := Or@@Flatten[{Thread[Abs[#[[1, 1, 1]] - pr[[1]]] < dx],
Thread[Abs[#[[1, 1, 2]] - pr[[2]]] < dy]}]&;
rhs := Or@@Flatten[{Thread[Abs[#[[1, -1, 1]] - pr[[1]]] < dx],
Thread[Abs[#[[1, -1, 2]] - pr[[2]]] < dy]}]&;
gg = gg /. x_Line?(lhs[#]&&rhs[#]&) :> {Arrowheads[{-ah, ah}], Arrow@@x};
gg = gg /. x_Line?lhs :> {Arrowheads[{-ah, 0}], Arrow@@x};
gg = gg /. x_Line?rhs :> {Arrowheads[{0, ah}], Arrow@@x};
gg
]
在一些等高线图上
ContourPlot[{x^2 + y^2 == 1, x^2 + y^2 == 6, x^3 + y^3 == {1, -1}},
{x, -2, 2}, {y, -2, 2}] // makeArrowPlot
失败的地方之一是在绘图边缘有水平线或垂直线
Plot[IntegerPart[x],{x,-2.5,2.5}]//makeArrowPlot[#,.03]&
这可以通过选项进行修复,例如PlotRange->{-2.1,2.1}
或排除->无
最后,最好添加一个选项,以便每个“曲线”只能在其边界上指向箭头。这将给出类似于贝里萨里乌斯答案中的情节(这也将避免上述问题)。但这是一个品味的问题。以下构造的优点是不会干扰图形结构的内部结构,并且比ask.sagemath中建议的构造更通用,因为它可以更好地管理绘图范围和无限
f[x_] = Gamma[x]
{plot, evals} =
Reap[Plot[f[x], {x, -2, 2}, Axes -> False, Frame -> True,
PlotRangePadding -> .2, EvaluationMonitor :> Sow[{x, f[x]}]]];
{{minX, maxX}, {minY, maxY}} = Options[plot, PlotRange] /. {_ -> y_} -> y;
ev = Select[evals[[1]], minX <= #[[1]] <= maxX && minY <= #[[2]] <= maxY &];
seq = SortBy[ev, #[[1]] &];
arr = {Arrow[{seq[[2]], seq[[1]]}], Arrow[{seq[[-2]], seq[[-1]]}]};
编辑
作为一项功能:
arrowPlot[f_, interval_] := Module[{plot, evals, within, seq, arr},
within[p_, r_] :=
r[[1, 1]] <= p[[1]] <= r[[1, 2]] &&
r[[2, 1]] <= p[[2]] <= r[[2, 2]];
{plot, evals} = Reap[
Plot[f[x], Evaluate@{x, interval /. List -> Sequence},
Axes -> False,
Frame -> True,
PlotRangePadding -> .2,
EvaluationMonitor :> Sow[{x, f[x]}]]];
seq = SortBy[Select[evals[[1]],
within[#,
Options[plot, PlotRange] /. {_ -> y_} -> y] &], #[[1]] &];
arr = {Arrow[{seq[[2]], seq[[1]]}], Arrow[{seq[[-2]], seq[[-1]]}]};
Show[plot, Graphics[{Red, arr}]]
];
arrowPlot[Gamma, {-3, 4}]
arrowPlot[f_,interval_]:=Module[{plot,evals,in,seq,arr},
在[p_u,r_u]内:=
r[[1,1]].2,
EvaluationMonitor:>Sow[{x,f[x]}]];
seq=排序方式[选择[evals[[1]],
在[#内,
选项[plot,PlotRange]/.{{u->y}->y]&],#[[1]]&];
arr={Arrow[{seq[[2]],seq[[1]}],Arrow[{seq[-2]],seq[-1]}]};
显示[绘图,图形[{Red,arr}]]
];
箭头图[Gamma,{-3,4}]
仍然在考虑什么对ListPlot&al更好。我可以建议找到由Plot生成的点/out@Alexey:我不太清楚你的意思…@Simon抱歉,这句话不完整。在不连续的情况下,我可以建议找到PlotRange
边缘上的点。例如:xmin=-2pi;xmax=Pi;图[Tan[x],{x,xmin,xmax},排除->{Cos[x]==0}]/。(1、1、1、1)以及Abs[[1、[1、[1、[1、1、1、1]-xmax.[1、[1、[1、1、1]-xmax.[1.[1、[1、[1、1、[1、1、1]-xmax]以及以及Abs[[1、[1、[1、[1、[1、1、1、1、1、1、1、1]-1]-xmax]以及以及-xmax.[1.[1.[1.[1.[1.[1.[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1、[1]1]1]1]1]以及1.[1]xmin]以及-6]以及-6]以及x]]}
@Alexey:我以为你就是这个意思。谢谢你的代码-如果你把它放在一个答案里,我会投赞成票。我也想到了这一点,但是如果绘图范围意味着一条线从绘图的顶部或底部退出,而不是从侧面退出,会发生什么呢?你必须从生成的绘图中提取PlotRange,并在规则中使用它…@Simon最近发现了这个(未经测试)@belisarius:几个月前有一个问题/答案,如何在Mathematica中制作带有箭头的传统3d轴,我再也找不到了,你还记得其中的任何关键字吗?@Yaro找到了啊,它在不同的网站,这就是为什么我找不到它…谢谢@贝里萨利斯:我完全错过了你的一系列修改,谢谢你的努力。看起来不错!不幸的是,问题中给出的轮廓图
排序失败,因为它是使用GraphicsComplex
对象构建的。这可以通过使用Normal
进行修复。但是你仍然会遇到这样的问题,你的规则不喜欢带有“工具提示”的绘图…@belisarius:我知道出了什么问题。Sequence[]
使得工具提示
获取的参数太多。用List[]
替换Sequence[]
,一切正常。酷!这将如何概括,也就是说,实验数据的列表图
?(假设您很懒,没有将数据放入插值函数,然后像上面那样使用Plot
)?
Show[plot, Graphics[{Red, arr}]]
arrowPlot[f_, interval_] := Module[{plot, evals, within, seq, arr},
within[p_, r_] :=
r[[1, 1]] <= p[[1]] <= r[[1, 2]] &&
r[[2, 1]] <= p[[2]] <= r[[2, 2]];
{plot, evals} = Reap[
Plot[f[x], Evaluate@{x, interval /. List -> Sequence},
Axes -> False,
Frame -> True,
PlotRangePadding -> .2,
EvaluationMonitor :> Sow[{x, f[x]}]]];
seq = SortBy[Select[evals[[1]],
within[#,
Options[plot, PlotRange] /. {_ -> y_} -> y] &], #[[1]] &];
arr = {Arrow[{seq[[2]], seq[[1]]}], Arrow[{seq[[-2]], seq[[-1]]}]};
Show[plot, Graphics[{Red, arr}]]
];
arrowPlot[Gamma, {-3, 4}]