\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
大致位于。)
当然,人们总是可以手动调整他们的位置和进一步的对齐等等,但这非常耗时。所以杰克通过使用额外的缩放样式提供了解决方案。
但这种做法“破坏”了其他一些事情,请参阅我的评论这里\xmin
。最值得注意的是,使用上述、 ... 命令时缺乏声音含义\xmax
。转换后,这些值超出范围,因此不再可见。
因此,我想定义一个新值或变量\xminSCALED
(和\xmaxSCALED
,\yminSCALED
和\ymaxSCALED
),该值或变量在坐标变换/缩放后被定义/更新。然后,这些新值应允许访问变换后的新的最小值和最大值X和是。
平均能量损失
\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}
显然,“最小”和“最大”并不是您真正想要的(我肯定漏掉了一些东西)。但也许这是正确的方向?
编辑
关于您的问题:
\edef\pgfmathresult{...}
表示“扩展定义”:它定义\pgfmathresult
为包含花括号中的所有内容。在您的示例中,您的意思是\pgfmathparse{...}
。请用 替换\edef\pgfmathresult
;\pgfmathparse
这样更容易使用,并且效果相同。我在使用该示例时遇到了编译问题。看来我的方法与xscalingdown 等
every axis/.style
不兼容groupplot
(抱歉没有正确测试). A workaround appears to be to add the
每个groupplot(我想有更聪明的方法,但我现在没有时间去寻找它们)。您的
*SCALED
版本应该类似于逆转换:如果您的转换是“*10”,那么 SCALED 版本应该使用“/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}