问题描述
给定 Graphics
对象,我如何确定包含所有图形所需的坐标范围?基本上我需要像 Show
默认情况下所做的事情,但我想明确指定 PlotRange
、PlotRangePadding
和 ImagePadding
.
Given Graphics
object, how do I determine the range of coordinates needed to include all of graphics? Basically I need something like what Show
does by default, but I want to specify PlotRange
,PlotRangePadding
and ImagePadding
explicitly.
例如,下面的两个Show
应该呈现相同的
Example, two Show
s below should render the same
g = Graphics[{Thickness[1], CapForm["Round"], Line[{{0, 0}, {1, 1}}]}];
Show[g]
Show[g, PlotRange -> getPlotRange[g], PlotRangePadding->getPlotRangePadding[g], ImagePadding->getImagePadding[g]]
动机:修复这个问题
更新:AbsoluteOptions
给了我 PlotRange
而不是其他两个选项.显式指定 ImagePadding->Automatic
会改变外观,尽管默认情况下它应该是 Automatic
.
Update:AbsoluteOptions
gives me PlotRange
but not the other two options. Explicitly specifying ImagePadding->Automatic
changes appearance though it's supposedly Automatic
by default.
下面两张图片显示不同,我不明白为什么
Two images below show differently and I don't understand why
g = Graphics[{Thickness[1], CapForm["Round"], Line[{{0, 0}, {1, 1}}]}];
Show[g]
Show[g, Sequence @@ AbsoluteOptions[Show[g]]]
更新 2:类似的问题是 一年前提出的,没有提出的解决方案,但自 Mathematica 8.0 起未修复.总结
Update 2:A similar problem was brought up a year ago, with no solutions proposed, and not fixed as of Mathematica 8.0. To summarize
- 没有办法通过
PlotRange
的显式设置来重现上面的 - 无法获得
Show[g]
使用的绝对 Show[g,PlotRange->Automatic]
看起来与Show[g]
不同AbsoluteOptions
可以给出错误的结果 用于PlotRange
Show[g]
ImagePadding
- There's no way to reproduce
Show[g]
above with explicit setting ofPlotRange
- There's no way to get absolute
ImagePadding
used byShow[g]
Show[g,PlotRange->Automatic]
looks different fromShow[g]
AbsoluteOptions
can give the wrong result forPlotRange
推荐答案
我可以建议以下 Ticks
hack:
I can suggest the following Ticks
hack:
pl = Plot[Sin[x], {x, 0, 10}];
Reap[Rasterize[Show[pl, Ticks -> {Sow[{##}] &, Sow[{##}] &}, ImageSize -> 0],
ImageResolution -> 1]][[2, 1]]
=> {{-0.208333, 10.2083}, {-1.04167, 1.04167}}
诀窍是真正的PlotRange
是由前端决定的,而不是由内核决定的.因此,我们必须强制前端渲染图形,以便评估刻度函数.这个 hack 给出了完整的 PlotRange
,并添加了 PlotRangePadding
的显式值.
The trick is that real PlotRange
is determined by the FrontEnd, not by the Kernel. So we must force the FrontEnd to render the graphics in order to get tick functions evaluated. This hack gives the complete PlotRange
with explicit value of PlotRangePadding
added.
更通用的解决方案,考虑到 pl
具有 DisplayFinction
选项的非标准值并且可能设置了 Axes
选项的可能性假
:
More general solution taking into account a possibility that pl
has non-standard value of DisplayFinction
option and that it may have Axes
option set to False
:
completePlotRange[plot:(_Graphics|_Graphics3D|_Graph)] :=
Quiet@Last@
Last@Reap[
Rasterize[
Show[plot, Axes -> True, Frame -> False, Ticks -> (Sow[{##}] &),
DisplayFunction -> Identity, ImageSize -> 0], ImageResolution -> 1]]
使用以下函数可以获得准确的PlotRange
(不添加PlotRangePadding
):
One can get the exact PlotRange
(without the PlotRangePadding
added) with the following function:
plotRange[plot : (_Graphics | _Graphics3D | _Graph)] :=
Quiet@Last@
Last@Reap[
Rasterize[
Show[plot, PlotRangePadding -> None, Axes -> True, Frame -> False,
Ticks -> (Sow[{##}] &), DisplayFunction -> Identity, ImageSize -> 0],
ImageResolution -> 1]]
附言在PlotRange
的文档页面上的更多信息"下,可以阅读:AbsoluteOptions
给出了 Mathematica 内部使用的选项的实际设置当给定的设置为 Automatic
或 All
时."(强调我的).因此,文档似乎甚至不能保证 AbsoluteOptions
在不是 Automatic
或 AllPlotRange
提供正确的值/代码>.
P.S. On the Documentation page for PlotRange
under the "More information" one can read: "AbsoluteOptions
gives the actual settings for options used internally by Mathematica when the setting given is Automatic
or All
. " (emphasis mine). So it seems that the Documentation does not even guarantee that AbsoluteOptions
will give correct values for PlotRange
when it is not Automatic
or All
.
这篇关于如何确定 PlotRange 以包含所有图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!