在Mathematica中绘制一个数字线

时间:2011-07-23 01:22:20

标签: wolfram-mathematica intervals

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

6 个答案:

答案 0 :(得分:10)

为了绘制开放或封闭的间隔,您可以执行以下操作:

intPlot[ss_, {s_, e_}, ee_] := Graphics[{Red, Thickness[.01],
   Text[Style[ss, Large, Red, Bold], {s, 0}],
   Text[Style[ee, Large, Red, Bold], {e, 0}],
   Line[{{s, 0}, {e, 0}}]},
  Axes -> {True, False},
  AxesStyle -> Directive[Thin, Blue, 12],
  PlotRange -> {{ s - .2 Abs@(s - e), e + .2 Abs@(s - e)}, {0, 0}},
  AspectRatio -> .1]

intPlot["[", {3, 4}, ")"]

enter image description here

修改

以下是@Simon完成的很好的扩展,可能会再次试图解决重叠间隔问题。

intPlot[ss_, {s_, e_}, ee_] := intPlot[{{ss, {s, e}, ee}}]
intPlot[ints : {{_String, {_?NumericQ, _?NumericQ}, _String} ..}] :=
 Module[{i = -1, c = ColorData[3, "ColorList"]},
  With[
   {min = Min[ints[[All, 2, 1]]], max = Max[ints[[All, 2, 2]]]},
   Graphics[Table[
     With[{ss = int[[1]], s = int[[2, 1]], e = int[[2, 2]], ee = int[[3]]}, 
       {c[[++i + 1]], Thickness[.01],
       Text[Style[ss, Large, c[[i + 1]], Bold], {s, i}], 
       Text[Style[ee, Large, c[[i + 1]], Bold], {e, i}],
       Line[{{s, i}, {e, i}}]}], {int, ints}], 
    Axes -> {True, False}, 
    AxesStyle -> Directive[Thin, Blue, 12], 
    PlotRange -> {{min - .2 Abs@(min - max), max + .2 Abs@(min - max)}, {0, ++i}}, 
    AspectRatio -> .2]]]

(*Examples*)

intPlot["[", {3, 4}, ")"]
intPlot[{{"(", {1, 2}, ")"}, {"[", {1.5, 4}, ")"}, 
        {"[", {2.5, 7}, ")"}, {"[", {1.5, 4}, ")"}}]

enter image description here

答案 1 :(得分:6)

这是使用RegionPlot的丑陋解决方案。开放限制用虚线表示,闭合限制用实线表示

numRegion[expr_, var_Symbol:x, range:{xmin_, xmax_}:{0, 0}, opts:OptionsPattern[]] :=
            Module[{le=LogicalExpand[Reduce[expr,var,Reals]],
                    y, opendots, closeddots, max, min, len},
 opendots =   Cases[Flatten[le/.And|Or->List], n_<var|n_>var|var<n_|var>n_:>n];
 closeddots = Cases[Flatten[le/.And|Or->List], n_<=var|n_>=var|var<=n_|var>=n_:>n];
 {max, min} = If[TrueQ[xmin < xmax], {xmin, xmax}, 
                 {Max, Min}@Cases[le, _?NumericQ, Infinity] // Through];
 len = max - min;
 RegionPlot[le && -1 < y < 1, {var, min-len/10, max+len/10}, {y, -1, 1},
            Epilog -> {Thick, Red, Line[{{#,1},{#,-1}}]&/@closeddots,
                       Dotted, Line[{{#,1},{#,-1}}]&/@opendots},
            Axes -> {True,False}, Frame->False, AspectRatio->.05, opts]]

减少绝对值的示例:

numRegion[Abs[x] < 2]

example 1

可以使用任何变量:

numRegion[0 < y <= 1 || y >= 2, y]

example 2

Reduce无关的不等式,比较以下内容:

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

example 3

答案 2 :(得分:6)

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

它依赖于LogicalExpand[Simplify@Reduce[expr, x]]Sort来使表达式成为类似于规范形式的替代规则可以处理的内容。这没有经过广泛测试,可能有点脆弱。例如,如果给定的expr缩减为TrueFalse,我的代码就不会优雅地消失。

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会自动重新调整箭头的宽度与包围图形的宽度,它给了我太多麻烦。)

好的,一些例子:

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

enter image description here
enter image description here
enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

编辑:让我们将上述内容与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"}]

output of the above

注意(在Mathematica会话或W | A网站上查看上述内容)时,重要点和灰色动态网格线上的花哨工具提示。我偷了这些想法,并将它们合并到上面编辑过的numLine[]代码中。

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

获取图形对象
wa = WolframAlpha["x>0", {{"NumberLine", 1}, "Content"}]
例如,你可以运行

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

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

aligned

答案 3 :(得分:3)

从Mathematica 10开始,有NumberLinePlot可用。

答案 4 :(得分:1)

以前的丑陋解决方案帮助我开发了InequalityPlot函数来解决和绘制两个变量中的不等式。

InequalityPlot[ineq_, {x_Symbol, xmin_, xmax_},{y_Symbol, ymin_, ymax_},
  opts : OptionsPattern[Join[Options[ContourPlot],
    Options[RegionPlot], {CurvesColor -> RGBColor[1, .4, .2]}]]] :=
 Module[{le = LogicalExpand[ineq], opencurves, closedcurves, curves},
  opencurves = Cases[Flatten[{le /. And | Or -> List}],
   lexp_ < rexp_ | lexp_ > rexp_ | lexp_ < rexp_ | lexpr_ > rexp_ :>
    {lexp == rexp, Dashing[Medium]}];
  closedcurves = Cases[Flatten[{le /. And | Or -> List}],
   lexp_ <= rexp_ | lexp_ >= rexp_ | lexp_ <= rexp_ | lexp_ >= rexp_ :>
    {lexp == rexp, Dashing[None]}];
  curves = Join[opencurves, closedcurves];
  Show[  RegionPlot[ineq, {x, xmin, xmax}, {y, ymin, ymax},
    BoundaryStyle -> None,
    Evaluate[Sequence @@ FilterRules[{opts}, Options[RegionPlot]]]],
   ContourPlot[First[#] // Evaluate, {x, xmin, xmax}, {y, ymin, ymax},
      ContourStyle -> Directive[OptionValue[CurvesColor], Last[#]],
      Evaluate[Sequence @@ FilterRules[{opts},
         Options[ContourPlot]]]] & /@ curves  ]
 ]

这里有两个例子:

InequalityPlot[0.5 <= x^2 + y^2 < 1, {x, -1, 1}, {y, -1, 1}]

enter image description here

InequalityPlot[x^2 + y^2 < 0.5 && x + y <= 0.5,{x, -1, 1}, {y, -1, 1}]

enter image description here

答案 5 :(得分:-1)

执行常规Plot,并设置Axes -> {True, False}(并隐藏边界框(如果存在,通常不存在)。根据需要调整图像大小或宽高比。

e.g。

Plot[
    Piecewise[{
        {0, And[0<x, x<1]}
    }],
    {x,-1,2},
    Axes -> {True, False}        
]

您可以使用Show将其与开放和闭合点的想象结合起来。

如果你没有正确设置你的Indeterminate或其他特殊值作为第二个参数传递给Piecewise(或者默认为0),你很可能不得不传入PlotRange -> {{-1,2},{-.1,.1}}或其他特殊值线宽或类似的绘图风格;或者,或者更确切地说,将值设置为999和{{1}}。