Wolfram mathematica 如何在Mathematica中获得Wolfram | Alpha 2D图形的十字线行为?

Wolfram mathematica 如何在Mathematica中获得Wolfram | Alpha 2D图形的十字线行为?,wolfram-mathematica,wolframalpha,Wolfram Mathematica,Wolframalpha,当鼠标光标位于Wolfram | Alpha中的2D绘图上时,会出现一对灰线,帮助您读取x轴和y轴上的坐标 ,我将鼠标放在下面Airy函数图中的一个转折点上 也可以在Mathematica内部使用 WolframAlpha["Plot Ai(x)", {{"Plot", 1}, "Content"}] 它还有一个额外的优点,就是某种显示坐标的定位器 如何在普通的Mathematica图形/绘图中模拟这种行为?来自Jens Peer Kuska: Manipulate[myPosition

当鼠标光标位于Wolfram | Alpha中的2D绘图上时,会出现一对灰线,帮助您读取x轴和y轴上的坐标 ,我将鼠标放在下面Airy函数图中的一个转折点上

也可以在Mathematica内部使用

WolframAlpha["Plot Ai(x)", {{"Plot", 1}, "Content"}]

它还有一个额外的优点,就是某种显示坐标的定位器


如何在普通的Mathematica图形/绘图中模拟这种行为?

来自Jens Peer Kuska:

Manipulate[myPosition = p;
 Plot[Sin[x], {x, 0, Pi}, 
  Epilog -> {Point[p], Text[p, p + {0.4, 0}]}], {{p, {0, 0}}, 
  Locator}]
马克·麦克卢尔:

labeledPointPlot[g_Graphics] := 
  Manipulate[
   Column[{Show[{g, Graphics@Point[pt]}], pt}], {pt, 
    Sequence @@ Transpose[PlotRange /. FullOptions[g]], Locator}];

labeledPointPlot[Plot[x^2, {x, -2, 2}]]
我找到了上面代码的来源,我以前写过:


以下是您在评论中要求的功能之一:

locatorPlot[func_, r : {var_, __}, other___] :=
 LocatorPane[
   Dynamic[pos, (pos = {#, func /. var -> #}) & @@ # &],
   Column[{Plot[func, r, other], Dynamic@pos}],
   AutoAction -> True,
   Appearance ->
     Graphics[{Gray, Line @ {{{-1, 0}, {1, 0}}, {{0, -1}, {0, 1}}}},
       ImageSize -> Full]
 ]

locatorPlot[AiryAi[z], {z, -11, 5}, ImageSize -> 400]


下面是一个相当笨拙的更新来处理您的新请求:

locatorPlot[func_List, r : {var_, __}, other___] :=
 DynamicModule[{pos, pos2},
  LocatorPane[
   Dynamic[pos, (pos = #; (pos2 = {#, First@Nearest[func /. var -> #, #2]}) & @@ #) &],
   Plot[func, r, other,
     Epilog ->
      {Text[\[GrayCircle], Dynamic@pos2], Text[Dynamic@pos2, Dynamic@pos2, {-1.2, 0}]}
   ],
   AutoAction -> True,
   Appearance -> 
     Graphics[{Gray, Line@{{{-1, 0}, {1, 0}}, {{0, -1}, {0, 1}}}}, ImageSize -> Full]
   ]
  ]

locatorPlot[{AiryAi[z], Sin[z]}, {z, -11, 5}, ImageSize -> 400]

这是我的版本,其行为与Wolfram | Alpha输出类似,只是它处理多个绘图。在W | A图形中,圆和文本跳转到最近的曲线,当光标不在图形上时完全消失。 最好是添加缺少的功能,也许可以使代码更灵活

WAPlot[fns_, range : {var_Symbol, __}] := 
 DynamicModule[{pos, fn = fns},
  If[Head[fn] === List, fn = First[Flatten[fn]]];
  LocatorPane[Dynamic[pos, (pos = {var, fn} /. var -> #[[1]]) &], 
   Plot[fns, range, Method -> {"GridLinesInFront" -> True},
    GridLines->Dynamic[{{#,Gray}}&/@MousePosition[{"Graphics",Graphics},None]]],
   AutoAction -> True, 
   Appearance -> Dynamic[Graphics[{Circle[pos, Scaled[.01]], 
       Text[Framed[Row[pos, ", "], RoundingRadius -> 5, 
         Background -> White], pos, {-1.3, 0}]}]]]]
然后,例如

WAPlot[{{AiryAi[x], -AiryAi[x]}, AiryBi[x]}, {x, -10, 2}]


这是一个新版本,它使用
MousePosition
代替
LocatorPane
,并窃取W先生的代码,使圆移动到最近的曲线。 该行为现在几乎与
WolframAlpha
输出相同

WAPlot[fns_, range : {var_Symbol, __}] := 
 DynamicModule[{fnList = Flatten[{fns}]}, Plot[fnList, range,
   GridLines -> 
    Dynamic[{{#, Gray}} & /@ MousePosition[{"Graphics", Graphics}]],
   Method -> {"GridLinesInFront" -> True},
   Epilog -> Dynamic[With[{mp = MousePosition[{"Graphics", Graphics}, None]},
      If[mp === None, {}, 
       With[{pos = {#1, First@Nearest[fnList /. var -> #1, #2]}& @@ mp},
        {Text[Style["\[EmptyCircle]", Medium, Bold], pos], 
         Text[Style[NumberForm[Row[pos, ", "], 2], Medium], pos, 
          {If[First[MousePosition["GraphicsScaled"]] < .5, -1.3, 1.3], 0}, 
          Background -> White]}]]]]
   ]]
WAPlot[fns,范围:{var\u Symbol,{uuuu}]:=
动态模块[{fnList=PLANT[{fns}]},绘图[fnList,范围,
网格线->
动态[{{#,Gray}}&/@MousePosition[{“Graphics”,Graphics}]],
方法->{“GridLinesInFront”->True},
Epilog->Dynamic[With[{mp=MousePosition[{“Graphics”,Graphics},None]},
如果[mp==None,{},
用[{pos={#1,First@Nearest[fnList/.var->#1,#2]}&@@mp},
{Text[Style[“\[EmptyCircle]”,中粗体,pos],
文本[Style[NumberForm[Row[pos,,“],2],Medium],pos,
{如果[第一个[鼠标位置[“GraphicsScaled”]]<.5,-1.3,1.3],0},
背景->白色]}]]]
]]

输出看起来与以前的版本非常相似,所以我不会发布屏幕截图

下面是另一种使用
最近的
的方法,与Simon的有点不同:

plot = Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi}];
With[{nf = Nearest[Flatten[Cases[Normal[plot], Line[p_, ___] :> p, Infinity], 1]]},
   Show[plot, 
      Epilog -> 
         Dynamic[DynamicModule[{
            pt = First[nf[MousePosition[{"Graphics", Graphics}, {0, 0}]]], 
            scaled = Clip[MousePosition[{"GraphicsScaled", Graphics}, {0, 0}], {0, 1}]
            }, 
           {
            {If[scaled === None, {}, 
               {Lighter@Gray, Line[{
                   {Scaled[{scaled[[1]], 1}], Scaled[{scaled[[1]], 0}]}, 
                   {Scaled[{1, scaled[[2]]}], Scaled[{0, scaled[[2]]}]}
                   }]
               }]}, 
            {AbsolutePointSize[7], Point[pt], White, AbsolutePointSize[5], Point[pt]},
            Text[Style[NumberForm[Row[pt, ", "], {5, 2}], 12, Background -> White], Offset[{7, 0}, pt], {-1, 0}]}
         ]]
    ]
 ]


这是从我的例子放在一起。(我不喜欢自由浮动落差线与点跟踪相结合;两者都感觉不错。)

嗨,W先生,我更喜欢灰色的“十字准线”,而不是显示坐标的定位器。。。很抱歉,这个问题不清楚。另外,
WolframAlpha
产生的定位器有并遵循曲线…@Simon,我无法尝试该函数,所以我不知道。请看下面我的新答案。我来看看我能对实际线路做些什么。@Simon,好的,我知道了。马上更新。我肯定你知道这一点,但以防万一你不知道:右键单击图形并选择“获取坐标”。您甚至可以通过单击标记点,然后复制它们。这已经从6之前的版本开始提供。(假设你的目的是以交互方式读取坐标。)@Szabolcs:谢谢,我确实知道,但值得再次指出!我主要是想模仿W | A代码。我有自己的代码(我稍后会发布),它可以完成大部分工作,但除其他外,它不能处理多个图形以及W | A代码。只需对来自
WolframAlpha[…]
的结果使用
InputForm
。@Brett:这就是我从(使用)那里偷取了动态
网格线的想法。然而,从那时起,
InputForm
似乎变得更加混乱了——我想,随着他们增加了更多的功能,使其能够处理更多的情况……我偶然发现了这一点。尝试评估
实验性的`探索[Plot]
+1太好了!这与W | A行为不同,但没关系。您能否模拟W | A代码在多个函数的绘图中跳转到最近的图形的方式?试着运行
WolframAlpha[“Ai(x),Bi(x)”,{{“Plot”,1},“Content”}]
看看我在说什么。@Simon对不起,我没有这个功能。但是,您是说定位器“捕捉”到最近的打印线,对吗?我忘了您有一个旧版本。是的,灰线跟随鼠标位置,但圆圈和文本捕捉到曲线。你认为最好的是什么。我只是想得到一些好的、干净的、灵活的代码,可以做一些类似于W | A的东西。我将发布与W | A行为接近的代码,除了它对多个图形的处理。@Simon,哦,错过了你自己的帖子。我最好快看一下。:-)请告诉我你问这个问题的时候没有这个密码,否则我就开枪打你-p@Mr.Wizard字体我把它分成了几部分。。。只是需要组装一下。我想我们生活在地球的另一边是件好事!我想你可以修改我发布的
最近的
等,使其功能化。如果您有问题,请告诉我。+1我喜欢在已生成的图形中获得最近的
点的方式-这意味着对于数值计算速度较慢的函数,您只需在绘图时计算一次。