在Mathematica中绘制数字线 [英] Plotting a number line in Mathematica

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

问题描述

我想在Mathematica中的数字线上绘制一个简单的间隔.我该怎么做?

I would like to plot a simple interval on the number line in Mathematica. How do I do this?

推荐答案

这是另一种尝试用更传统的白色和黑色圆圈绘制数字线的方法,尽管您想要的任何图形元素都可以轻松换出.

Here's another attempt that draws number lines with the more conventional white and black circles, although any graphics element that you want can be easily swapped out.

依靠LogicalExpand[Simplify@Reduce[expr, x]]Sort将表达式转换为类似于替换规则可以使用的规范形式的表达式.这未经广泛测试,可能有点脆弱.例如,如果给定的expr减少为TrueFalse,我的代码将无法正常死掉.

It relies on LogicalExpand[Simplify@Reduce[expr, x]] and Sort to get the expression into something resembling a canonical form that the replacement rules can work on. This is not extensively tested and probably a little fragile. For example if the given expr reduces to True or False, my code does not die gracefully.

numLine[expr_, x_Symbol:x, range:{_, _}:{Null, Null}, 
  Optional[hs:_?NumericQ, 1/30], opts:OptionsPattern[]] := 
 Module[{le = {LogicalExpand[Simplify@Reduce[expr, x]]} /. Or -> List,
   max, min, len, ints = {}, h, disk, hArrow, lt = Less|LessEqual, gt = Greater|GreaterEqual},
  If[TrueQ@MatchQ[range, {a_, b_} /; a < b],
   {min, max} = range,
   {min, max} = Through[{Min, Max}@Cases[le, _?NumericQ, \[Infinity]]]];
  len =Max[{max - min, 1}]; h = len hs;
  hArrow[{x1_, x2_}, head1_, head2_] := {{Thick, Line[{{x1, h}, {x2, h}}]},
                                         Tooltip[head1, x1], Tooltip[head2, x2]};
  disk[a_, ltgt_] := {EdgeForm[{Thick, Black}], 
    Switch[ltgt, Less | Greater, White, LessEqual | GreaterEqual, Black], 
    Disk[{a, h}, h]};
  With[{p = Position[le, And[_, _]]}, 
       ints = Extract[le, p] /. And -> (SortBy[And[##], First] &); 
       le = Delete[le, p]];   
  ints = ints /. (l1 : lt)[a_, x] && (l2 : lt)[x, b_] :> 
     hArrow[{a, b}, disk[a, l1], disk[b, l2]];
  le = le /. {(*_Unequal|True|False:>Null,*)
     (l : lt)[x, a_] :> (min = min - .3 len; 
       hArrow[{a, min}, disk[a, l], 
        Polygon[{{min, 0}, {min, 2 h}, {min - Sqrt[3] h, h}}]]),
     (g : gt)[x, a_] :> (max = max + .3 len; 
       hArrow[{a, max}, disk[a, g], 
        Polygon[{{max, 0}, {max, 2 h}, {max + Sqrt[3] h, h}}]])};
  Graphics[{ints, le}, opts, Axes -> {True, False}, 
   PlotRange -> {{min - .1 len, max + .1 len}, {-h, 3 h}},
   GridLines -> Dynamic[{{#, Gray}} & /@ MousePosition[
                           {"Graphics", Graphics}, None]], 
   Method -> {"GridLinesInFront" -> True}]
  ]

(注意:我本来尝试使用ArrowArrowheads画线-但由于Arrowheads会根据周围图形的宽度自动缩放箭头,这让我头疼不已)

(Note: I had originally tried to use Arrow and Arrowheads to draw the lines - but since Arrowheads automatically rescales the arrow heads with respect to the width of the encompassing graphics, it gave me too many headaches.)

好,下面是一些例子:

numLine[0 < x], 
numLine[0 > x]
numLine[0 < x <= 1, ImageSize -> Medium]





numLine[0 < x <= 1 || x > 2, Ticks -> {{0, 1, 2}}]

numLine[x <= 1 && x != 0, Ticks -> {{0, 1}}]

GraphicsColumn[{
  numLine[0 < x <= 1 || x >= 2 || x < 0],
  numLine[0 < x <= 1 || x >= 2 || x <= 0, x, {0, 2}]
  }]

编辑:让我们将以上内容与 Wolfram | Alpha

Let's compare the above to the output of Wolfram|Alpha

WolframAlpha["0 < x <= 1 or x >= 2 or x < 0", {{"NumberLine", 1}, "Content"}]
WolframAlpha["0 < x <= 1 or x >= 2 or x <= 0", {{"NumberLine", 1}, "Content"}]

请注意(在Mathematica会话中或在W | A网站上查看以上内容时)有关要点和灰色动态网格线的奇特工具提示.我偷走了这些想法,并将它们并入了上面编辑过的numLine[]代码中.

Note (when viewing the above in a Mathematica session or the W|A website) the fancy tooltips on the important points and the gray, dynamic grid lines. I've stolen these ideas and incorporated them into the edited numLine[] code above.

WolframAlpha的输出不是普通的Graphics对象,因此很难修改其Options或使用Show进行组合.要查看Wolfram | Alpha可以返回的各种数字线对象,请运行WolframAlpha["x>0", {{"NumberLine"}}]-内容",单元格"和输入"全部返回基本上相同的对象.无论如何,要从

The output from WolframAlpha is not quite a normal Graphics object, so it's hard to modify its Options or combine using Show. To see the various numberline objects that Wolfram|Alpha can return, run WolframAlpha["x>0", {{"NumberLine"}}] - "Content", "Cell" and "Input" all return basically the same object. Anyway, to get a graphics object from

wa = WolframAlpha["x>0", {{"NumberLine", 1}, "Content"}]

例如,您可以运行

Graphics@@First@Cases[wa, GraphicsBox[__], Infinity, 1]

然后我们可以修改图形对象并将它们组合在网格中以获得

Then we can modify the graphics objects and combine them in a grid to get

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

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