\ymin
我使用由原始 x 和 y 值組成的“混合”座標。使用起來很舒服,感謝貢薩洛:
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\xmin}[0]{\getpgfkey{xmin}}
\newcommand{\xmax}[0]{\getpgfkey{xmax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
所以現在......假設您有大量資料值,並且您希望對資料進行縮放並相應地調整刻度標籤的內容。 (這直接基於我的個人經驗:有人告訴我 的預設位置y tick labels
不是那麼漂亮。就我個人而言,我同意。他們的預設樣式將它們放在(0,1), above right
,粗略地說。)
當然,人們總是可以手動調整它們的位置並進一步對齊等等,但這非常耗時。因此,Jake 透過使用額外的縮放方式提供了解決方案。
但是這種“破壞”了其他一些東西,請參閱我的評論這裡。最值得注意的是使用上述\xmin
, ... 命令時缺乏合理的含義\xmax
。轉換後,這些值超出範圍,因此不再可見。
所以我想定義一個新的值或變數\xminSCALED
( 和\xmaxSCALED
,\yminSCALED
和\ymaxSCALED
),它在座標轉換/縮放後定義/更新。然後,這些新值應該可以存取轉換後的新的最小值和最大值X和y。
微量元素
\documentclass{standalone}
\usepackage{pgfplots}
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\xmin}[0]{\getpgfkey{xmin}}
\newcommand{\xmax}[0]{\getpgfkey{xmax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
\usepgfplotslibrary{groupplots}
\pgfplotsset{
YScaling/.style={
y coord trafo/.code=\pgfmathparse{##1/(1e#1)},
%the following 4 lines break the MWE, also not working are variants with \newcommand or \renewcommand
%\let{\pgfkeysvalueof{/pgfplots/xmin}}={\xminSCALED},
%\let{\pgfkeysvalueof{/pgfplots/xmax}}={\xmaxSCALED},
%\let{\pgfkeysvalueof{/pgfplots/ymin}}={\yminSCALED},
%\let{\pgfkeysvalueof{/pgfplots/ymax}}={\ymaxSCALED},
ylabel style={
append after command={
node [rotate=90, anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{#1}}$}
}
}
},
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
horizontal sep=0.2cm,
group size=2 by 1,
ylabels at=edge left,
yticklabels at=edge left,
},
ymin=300, ymax=1300,
xlabel={The label for the x-axis},
ylabel={Some y-values},
scale ticks above exponent={2},
]
\nextgroupplot[YScaling=3]
\addplot[only marks] coordinates{(2000,1200) (3000,500)};
\draw[ultra thick, dashed, blue] (axis cs:2000,\pgfkeysvalueof{/pgfplots/ymin}) rectangle (axis cs:3060,\pgfkeysvalueof{/pgfplots/ymax}); %<--- not working
\draw[ultra thick, dashed, red] (axis cs:2000,600) rectangle (axis cs:3050,1150); %<--- works but does not look equally good
%
\nextgroupplot[scaled y ticks=false]
\addplot[only marks] coordinates{(2000,1200) (4000,400)};
\addlegendentry{Text}
\end{groupplot}
\end{tikzpicture}
\end{document}
下面是附加 MWE 的圖片
額外的MWE
\documentclass{standalone}
\usepackage{pgfplots}
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\xmin}[0]{\getpgfkey{xmin}}
\newcommand{\xmax}[0]{\getpgfkey{xmax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
\usepgfplotslibrary{groupplots}
\pgfplotsset{
xscalingdown/.style={ %XXX SCALE DOWN
x coord trafo/.code=\pgfmathparse{##1/(1e#1)},
xlabel style={
append after command={
node [anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{#1}}$}
}
},
every axis/.append code={%
\pgfmathresult{\xmax/(1e#1)}\global\let\xmaxSCALED=\pgfmathresult
\pgfmathresult{\xmin/(1e#1)}\global\let\xminSCALED=\pgfmathresult
% \pgfmathresult{\ymax}\global\let\ymaxSCALED=\pgfmathresult
% \pgfmathresult{\ymin}\global\let\yminSCALED=\pgfmathresult
},
},
xscalingup/.style={ %XXX SCALE UP
x coord trafo/.code=\pgfmathparse{##1*(1e#1)},
xlabel style={
append after command={
node [anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{{-1}#1}}$}
}
},
every axis/.append code={%
\pgfmathresult{\xmax*(1e#1)}\global\let\xmaxSCALED=\pgfmathresult
\pgfmathresult{\xmin*(1e#1)}\global\let\xminSCALED=\pgfmathresult
%\pgfmathresult{\ymax}\global\let\ymaxSCALED=\pgfmathresult
%\pgfmathresult{\ymin}\global\let\yminSCALED=\pgfmathresult
},
},
yscalingdown/.style={ %YYY SCALE DOWN
y coord trafo/.code=\pgfmathparse{##1/(1e#1)},
ylabel style={
append after command={
node [rotate=90, anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{#1}}$}
}
},
every axis/.append code={%
\pgfmathparse{\ymax/(1e#1)}\global\let\ymaxSCALED=\pgfmathresult
\pgfmathparse{\ymin/(1e#1)}\global\let\yminSCALED=\pgfmathresult
%\pgfmathresult{\xmax}\global\let\xmaxSCALED=\pgfmathresult
%\pgfmathresult{\xmin}\global\let\xminSCALED=\pgfmathresult
},
},
yscalingup/.style={ %YYY SCALE UP
y coord trafo/.code=\pgfmathparse{##1*(1e#1)},
ylabel style={
append after command={
node [rotate=90, anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{{-1}#1}}$}
}
},
every axis/.append code={%
\pgfmathparse{\ymax*(1e#1)}\global\let\ymaxSCALED=\pgfmathresult
\pgfmathparse{\ymin*(1e#1)}\global\let\yminSCALED=\pgfmathresult
%\pgfmathresult{\xmax}\global\let\xmaxSCALED=\pgfmathresult
%\pgfmathresult{\xmin}\global\let\xminSCALED=\pgfmathresult
},
},
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
horizontal sep=0.2cm,
group size=2 by 1,
ylabels at=edge left,
yticklabels at=edge left,
},
ymin=-100, ymax=1300,
xlabel={The label for the x-axis},
ylabel={Some y-values},
xscalingdown=3,
yscalingdown=3,
]
\nextgroupplot
\addplot coordinates{(2000,1200) (3000,\yminSCALED)};
\filldraw[ultra thick, dashed, blue] (\xminSCALED,\yminSCALED) rectangle (\xmaxSCALED,\ymaxSCALED);
\nextgroupplot
\addplot+ coordinates{(2100,1000) (2800,1200)};
\filldraw[ultra thick, dashed, red] (axis cs:2100,1000) rectangle (axis cs:\xmaxSCALED,1000) node[below, fill=orange] {bla};
%%NOTABLE DIFFERENT OUTPUT when omitting 'axis cs'
\filldraw[ultra thick, dashed, red] (2100,1000) rectangle (axis cs:\xmaxSCALED,1000) node[below, fill=orange] {bla};
\end{groupplot}
\end{tikzpicture}
\end{document}
答案1
對於評論來說,這實際上太多了,但對於答案來說還不夠……但也許您可以找到感興趣的東西或提出更有針對性的問題來幫助我理解用例。
據我了解,您想使用\yminSCALED
來接受 內部的值axis cs
,對吧?換句話說,你想要 " yminSCALED
:= 逆y 變換 ( \ymin
)" ?
這可能會做到這一點:
\documentclass{standalone}
\usepackage{pgfplots}
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\xmin}[0]{\getpgfkey{xmin}}
\newcommand{\xmax}[0]{\getpgfkey{xmax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
\usepgfplotslibrary{groupplots}
\pgfplotsset{
YScaling/.style={
y coord trafo/.code=\pgfmathparse{##1/(1e#1)},
%the following 4 lines break the MWE, also not working are variants with \newcommand or \renewcommand
%\let{\pgfkeysvalueof{/pgfplots/xmin}}={\xminSCALED},
%\let{\pgfkeysvalueof{/pgfplots/xmax}}={\xmaxSCALED},
%\let{\pgfkeysvalueof{/pgfplots/ymin}}={\yminSCALED},
%\let{\pgfkeysvalueof{/pgfplots/ymax}}={\ymaxSCALED},
ylabel style={
append after command={
node [rotate=90, anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{#1}}$}
}
},
%
every axis/.append code={%
\pgfmathparse{\ymax*(1e#1)}\global\let\ymaxSCALED=\pgfmathresult
\pgfmathparse{\ymin*(1e#1)}\global\let\yminSCALED=\pgfmathresult
\edef\pgfmathresult{\xmax}\global\let\xmaxSCALED=\pgfmathresult
\edef\pgfmathresult{\xmin}\global\let\xminSCALED=\pgfmathresult
},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
group style={
horizontal sep=0.2cm,
group size=2 by 1,
ylabels at=edge left,
yticklabels at=edge left,
},
ymin=300, ymax=1300,
xlabel={The label for the x-axis},
ylabel={Some y-values},
scale ticks above exponent={2},
YScaling=3,
]
\addplot[only marks] coordinates{(2000,1200) (3000,500)};
\draw[ultra thick, dashed, blue] (axis cs:\xminSCALED,\yminSCALED) rectangle (axis cs:\xmaxSCALED,\ymaxSCALED); %<--- not working
%
\end{axis}
\end{tikzpicture}
\end{document}
顯然“min”和“max”並不是您真正想要的(我一定錯過了一些東西)。但也許這就是正確的方向?
編輯
關於您的問題:
\edef\pgfmathresult{...}
意思是「擴展定義」:它定義\pgfmathresult
包含大括號中的任何內容。在你的例子中,你的意思是\pgfmathparse{...}
.請替換\edef\pgfmathresult
為\pgfmathparse
;它更容易使用並且具有相同的效果。我在範例中遇到了編譯問題。看來我的方法與xscalingdown` 等
every axis/.style
不相容groupplot
(抱歉沒有正確測試它). A workaround appears to be to add the
每個groupplot (我想有更聰明的方法,但我現在沒有時間找到它們)。您的
*SCALED
版本應該類似於逆轉換:如果您的轉換是“*10”,則縮放版本應使用“/10”。我修復了 xlabel 對齊。
代碼:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\xmin}[0]{\getpgfkey{xmin}}
\newcommand{\xmax}[0]{\getpgfkey{xmax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
\usepgfplotslibrary{groupplots}
\pgfplotsset{
xscalingdown/.style={
x coord trafo/.code=\pgfmathparse{##1/(1e#1)},
every axis/.append code={%
\pgfmathparse{\xmax*(1e#1)}\global\let\xmaxSCALED=\pgfmathresult
\pgfmathparse{\xmin*(1e#1)}\global\let\xminSCALED=\pgfmathresult
},
xlabel style={
append after command={
node [ anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{#1}}$}
}
}
},
xscalingup/.style={
x coord trafo/.code=\pgfmathparse{##1*(1e#1)},
every axis/.append code={%
\pgfmathparse{\xmax/(1e#1)}\global\let\xmaxSCALED=\pgfmathresult
\pgfmathparse{\xmin/(1e#1)}\global\let\xminSCALED=\pgfmathresult
},
xlabel style={
append after command={
node [anchor=base west] at (\tikzlastnode.base west) {$\cdot{10^{{-1}#1}}$}
}
}
},
yscalingdown/.style={
y coord trafo/.code=\pgfmathparse{##1/(1e#1)},
every axis/.append code={%
\pgfmathparse{\ymax*(1e#1)}\global\let\ymaxSCALED=\pgfmathresult
\pgfmathparse{\ymin*(1e#1)}\global\let\yminSCALED=\pgfmathresult
},
ylabel style={
append after command={
node [rotate=90, anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{#1}}$}
}
}
},
yscalingup/.style={
y coord trafo/.code=\pgfmathparse{##1*(1e#1)},
every axis/.append code={%
\pgfmathparse{\ymax/(1e#1)}\global\let\ymaxSCALED=\pgfmathresult
\pgfmathparse{\ymin/(1e#1)}\global\let\yminSCALED=\pgfmathresult
},
ylabel style={
append after command={
node [rotate=90, anchor=base west] at (\tikzlastnode.base east) {$\cdot{10^{{-1}#1}}$}
}
}
},
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
horizontal sep=0.2cm,
group size=2 by 1,
ylabels at=edge left,
yticklabels at=edge left,
},
ymin=-100, ymax=1300,
xlabel={The label for the x-axis},
ylabel={Some y-values},
]
\nextgroupplot[
xscalingdown=3,
yscalingdown=3,
]
\addplot[only marks] coordinates{(2000,1200) (3000,500)};
\draw[ultra thick, dashed, blue] (axis cs:\xminSCALED,\yminSCALED) rectangle (axis cs:\xmaxSCALED,\ymaxSCALED);
\nextgroupplot[
xscalingdown=3,
yscalingdown=3,
]
\addplot[only marks] coordinates{(2100,1000) (2800,1200)};
\draw[ultra thick, dashed, blue] (axis cs:\xminSCALED,\yminSCALED) rectangle (axis cs:\xmaxSCALED,\ymaxSCALED);
\end{groupplot}
\end{tikzpicture}
\end{document}