是否可以在Mathematica中檢索自動生成的繪圖範圍?檢索自動生成的繪圖範圍
例如,如果我要做到:
Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotRange -> Automatic]
然後我想知道的是,Y軸的範圍爲-1到1和X軸的範圍爲0〜2 PI。
是否可以在Mathematica中檢索自動生成的繪圖範圍?檢索自動生成的繪圖範圍
例如,如果我要做到:
Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotRange -> Automatic]
然後我想知道的是,Y軸的範圍爲-1到1和X軸的範圍爲0〜2 PI。
p = Plot[Sin[x], {x, 0, 2*Pi}, PlotRange -> Automatic];
工作了這一點看AbsoluteOptions
是有點彩票,但在這種情況下工作
AbsoluteOptions[p, PlotRange]
{PlotRange -> {{0., 6.28319}, {-1., 1.}}}
即使AbsoluteOptions
所取代FullOptions
有時它也是值得嘗試FullOptions
如果當AbsoluteOptions
失敗,因爲我所遇到的情況下,當AbsoluteOptions
失敗,但FullOptions
作品。在這種情況下FullOptions
也適用:
FullOptions[p, PlotRange]
{{0., 6.28319}, {-1., 1.}}
不漂亮或一般,但你可以蠻力它喜歡這樣:
p = Plot[Sin[x], {x, 0, 2*Pi}, PlotRange -> Automatic];
[email protected][p, List[___, Rule[PlotRange, x_], ___] -> x]
給
{{0., 6.28319}, {-1., 1.}}
您可以在FullForm[p]
使用AbsoluteOptions
功能,Q。 v。在文檔中。
In[56]:= x = Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotRange -> Automatic];
AbsoluteOptions[x, PlotRange]
Out[57]= {PlotRange -> {{0., 6.28319}, {-1., 1.}}}
ACL一樣,我經常深入到與位置FullForm進行後處理圖形:
例如查找和修改PlotRange:
p = Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotRange -> Automatic];
rpos = Position[p, PlotRange];
Print["Initial PlotRange"];
p[[Sequence @@ Most[First[rpos]]]]
Print["Modified PlotRange"];
p[[Sequence @@ Most[First[rpos]]]] = PlotRange -> {{0, Pi}, {-1, 1}}
Print[p]
或者,修改顏色:
p = Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotRange -> Automatic];
hpos = Position[p, Hue];
Print["Initial colours"]
p[[Sequence @@ Most[#]]] & /@ hpos
Print["New colours"]
MapThread[(p[[Sequence @@ Most[#1]]] = #2) &, {hpos, {Green, Orange}}]
Print[p]
我可以建議以下Ticks
黑客:
pl = Plot[Sin[x], {x, 0, 10}];
Reap[Rasterize[Show[pl, Ticks -> {Sow[{##}] &, Sow[{##}] &}],
ImageResolution -> 1]][[2, 1]]
=> {{-0.208333, 10.2083}, {-1.04167, 1.04167}}
的訣竅是真正的PlotRange
由前端確定,而不是內核。因此,我們必須強制FrontEnd渲染圖形以獲得評估的滴答函數。這個黑客給出了完整的PlotRange
明確的值增加了PlotRangePadding
。
更通用的解決方案考慮一種可能性,即pl
有DisplayFinction
選擇非標值,它可能已經Axes
選項設置爲False
:
completePlotRange[plot_] :=
[email protected]@
Reap[Rasterize[
Show[plot, Ticks -> (Sow[{##}] &), Axes -> True,
DisplayFunction -> Identity], ImageResolution -> 1]]
在下面的文檔頁面PlotRange
關於AbsoluteOptions
:「AbsoluteOptions
」的「更多信息」可以讀取一個重要說明:PlotRange
規範給出明確形式Automatic
se給予「(突出顯示是我的)。因此,對於所有座標,文檔不保證AbsoluteOptions
將爲PlotRange
提供正確的值,而不是Automatic
。
嗨Alexey,我們大多數人現在主要活躍在新網站上,請看這裏:http://mathematica.stackexchange.com/如果你也可以加入,那將會很棒! – Szabolcs 2012-02-07 11:34:05
@Szabolcs謝謝,我已經在那裏。 – 2012-02-07 11:42:06
賓果!這是比我剛剛給出的更完整的答案。 – kkm 2012-01-16 01:11:42
這正是我所期待的。謝謝! – Cassini 2012-01-16 01:14:37
對於'p'的'FullForm'中的簡單模式匹配'PlotRange',你有什麼看法?我以前遇到過AbsoluteOptions問題(可能是因爲不瞭解它是如何工作的),所以我現在傾向於避免使用它來支持暴力模式匹配,就像我的答案一樣。 – acl 2012-01-16 01:41:17