作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Mathematica 的数轴上绘制一个简单的区间。我该怎么做呢?
最佳答案
这是使用更传统的白色和黑色圆圈绘制数字线的另一种尝试,尽管您可以轻松替换您想要的任何图形元素。
它依赖于 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}]
]
Arrow
和
Arrowheads
来绘制线条 - 但由于
Arrowheads
会根据包含图形的宽度自动重新缩放箭头,这让我很头疼。)
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}]
}]
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"}]
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]
关于wolfram-mathematica - 在 Mathematica 中绘制数轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6797651/
我正在使用 JFreeChart 显示正态分布,并将刻度数更改为标准差;但我也希望在滴答声出现的中间始终存在“平均”值(value)。交叉发布here . 所以标准偏差 = 2 ;均值 = 1 -3
我是一名优秀的程序员,十分优秀!