Jam*_*ard 10 wolfram-mathematica intervals
我想在Mathematica的数字线上绘制一个简单的间隔.我该怎么做呢?
Dr.*_*ius 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}, ")"]
Run Code Online (Sandbox Code Playgroud)

编辑
以下是@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}, ")"}}]
Run Code Online (Sandbox Code Playgroud)

这是一个丑陋的解决方案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]]
Run Code Online (Sandbox Code Playgroud)
减少绝对值的示例:
numRegion[Abs[x] < 2]
Run Code Online (Sandbox Code Playgroud)

可以使用任何变量:
numRegion[0 < y <= 1 || y >= 2, y]
Run Code Online (Sandbox Code Playgroud)

Reduce无关的不等式,比较如下:
GraphicsColumn[{numRegion[0 < x <= 1 || x >= 2 || x < 0],
numRegion[0 < x <= 1 || x >= 2 || x <= 0, x, {0, 2}]}]
Run Code Online (Sandbox Code Playgroud)

这是另一种使用更常规的白色和黑色圆圈绘制数字线的尝试,尽管您想要的任何图形元素都可以轻松换出.
它依赖于LogicalExpand[Simplify@Reduce[expr, x]]并将Sort表达式转换为类似于规范形式的替代规则可以起作用的东西.这没有经过广泛测试,可能有点脆弱.例如,如果给定的expr减少为True或False,我的代码不会优雅地死亡.
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}]
]
Run Code Online (Sandbox Code Playgroud)
(注意:我原本试图使用Arrow和Arrowheads绘制线条 - 但由于Arrowheads箭头头部相对于包围图形的宽度自动重新缩放,因此给我带来了太多麻烦.)
好的,一些例子:
numLine[0 < x],
numLine[0 > x]
numLine[0 < x <= 1, ImageSize -> Medium]
Run Code Online (Sandbox Code Playgroud)



numLine[0 < x <= 1 || x > 2, Ticks -> {{0, 1, 2}}]
Run Code Online (Sandbox Code Playgroud)

numLine[x <= 1 && x != 0, Ticks -> {{0, 1}}]
Run Code Online (Sandbox Code Playgroud)

GraphicsColumn[{
numLine[0 < x <= 1 || x >= 2 || x < 0],
numLine[0 < x <= 1 || x >= 2 || x <= 0, x, {0, 2}]
}]
Run Code Online (Sandbox Code Playgroud)

编辑: 让我们将上面的内容与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"}]
Run Code Online (Sandbox Code Playgroud)

注意(在Mathematica会话或W | A网站上查看以上内容时)重要点和灰色动态网格线上的花哨工具提示.我偷了这些想法并将它们合并到numLine[]上面编辑的代码中.
输出WolframAlpha不是一个普通的Graphics对象,所以很难修改它Options或组合使用Show.要查看Wolfram | Alpha可以返回的各种数字对象,运行WolframAlpha["x>0", {{"NumberLine"}}]- "内容","单元格"和"输入"都返回基本相同的对象.无论如何,要从中获取图形对象
wa = WolframAlpha["x>0", {{"NumberLine", 1}, "Content"}]
Run Code Online (Sandbox Code Playgroud)
例如,你可以跑
Graphics@@First@Cases[wa, GraphicsBox[__], Infinity, 1]
Run Code Online (Sandbox Code Playgroud)
然后我们可以修改图形对象并将它们组合在一个网格中来获取

| 归档时间: |
|
| 查看次数: |
4491 次 |
| 最近记录: |