PGFplots:计算标题旋转角度,使其与明显较长的轴平行

PGFplots:计算标题旋转角度,使其与明显较长的轴平行

为了使标题与明显较长的轴平行,如何计算两个标题(即\FirstTitleAngle\SecondTitleAngle)的旋转角度,而不是手动试验?

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
    \newcommand{\FirstTitleAngle}{5}
    \begin{tikzpicture}
        \begin{axis}[
        title=some long long long  title,
        view={75}{30},
        title style={rotate=\FirstTitleAngle}
        ]
        \addplot3 [
            surf,
            domain=-2:2,
            domain y=-1.3:1.3,
            ] {exp(-x^2-y^2)*x};
        \end{axis}
    \end{tikzpicture}

    \newcommand{\SecondTitleAngle}{-3}
    \begin{tikzpicture}
    \begin{axis}[
    title=some long long long  title,
    view={-80}{20},
    title style={rotate=\SecondTitleAngle}
    ]
    \addplot3 [
    surf,
    domain=-2:2,
    domain y=-1.3:1.3,
    ] {exp(-x^2-y^2)*x};
    \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述


此外,对于 groupplots,在 global 的情况下view={}{},如何计算角度并进行全局分配?

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots,mathtools}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
    group name=G,
    group size=2 by 3,
}, xtick=\empty, ytick=\empty, ztick=\empty, view={75}{30}
]
\nextgroupplot[title=some long long long ong title]
\addplot3 [
surf,
domain=-2:2,
domain y=-1.3:1.3,
] {exp(-x^2-y^2)*x};
%
\nextgroupplot[title=some long long long ong title]
\addplot3 [
surf,
domain=-2:2,
domain y=-1.3:1.3,
] {exp(-x^2-y^2)*x};
\end{groupplot}
\end{tikzpicture}
\end{document}

答案1

您需要做的title就是将包含该内容的节点放置在轴形状上。如果您愿意使用 Ti 执行此操作Z,您可以使用库来计算角度calc。您仍然必须自己决定是否希望标题与 x 轴或 y 轴平行,这决定了坐标的选择,并且在某些方向上与自然选择相差 180 度,这就是为什么第二个示例中x1-180。请忘掉这一切。这里有两种分别确定 x 轴和 y 轴旋转角度的样式。

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\pgfplotsset{find y axis rotation/.style={
before end axis/.code={\path (0,0,0) coordinate (x0) (0,1,0) coordinate (x1);
\path let
\p1=($(x1)-(x0)$),\n1={ifthenelse(\x1>0,atan2(\y1,\x1),atan2(\y1,\x1)-180)} in 
\pgfextra{\xdef#1{\n1}};}
},
find x axis rotation/.style={
before end axis/.code={\path (0,0,0) coordinate (x0) (1,0,0) coordinate (x1);
\path let
\p1=($(x1)-(x0)$),\n1={ifthenelse(\x1>0,atan2(\y1,\x1),atan2(\y1,\x1)-180)} in 
\pgfextra{\xdef#1{\n1}};}
},
}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
        title=some long long long  title,
        view={75}{30},
        find y axis rotation=\myangle,
        title style={rotate=\myangle}
        ]
        \addplot3 [
            surf,
            domain=-2:2,
            domain y=-1.3:1.3,
            ] {exp(-x^2-y^2)*x};

        \end{axis}

    \end{tikzpicture}

    \begin{tikzpicture}
    \begin{axis}[
        find y axis rotation=\myangle,
        title=some long long long  title,
        view={-80}{20},
        title style={rotate=\myangle} 
    ]
    \addplot3 [
    surf,
    domain=-2:2,
    domain y=-1.3:1.3,
    ] {exp(-x^2-y^2)*x};
    \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

附录:至于您关于群体图的问题:它的工作原理是一样的。

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots,mathtools}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\pgfplotsset{find y axis rotation/.style={
before end axis/.code={\path (0,0,0) coordinate (x0) (0,1,0) coordinate (x1);
\path let
\p1=($(x1)-(x0)$),\n1={ifthenelse(\x1>0,atan2(\y1,\x1),atan2(\y1,\x1)-180)} in 
\pgfextra{\xdef#1{\n1}};}
},
find x axis rotation/.style={
before end axis/.code={\path (0,0,0) coordinate (x0) (1,0,0) coordinate (x1);
\path let
\p1=($(x1)-(x0)$),\n1={ifthenelse(\x1>0,atan2(\y1,\x1),atan2(\y1,\x1)-180)} in 
\pgfextra{\xdef#1{\n1}};}
},
}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
    group name=G,
    group size=2 by 3,
}, xtick=\empty, ytick=\empty, ztick=\empty, view={75}{30}
]
\nextgroupplot[title=some long long long ong title,
find y axis rotation=\myangle,title style={rotate=\myangle}]
\addplot3 [
surf,
domain=-2:2,
domain y=-1.3:1.3,
] {exp(-x^2-y^2)*x};
%
\nextgroupplot[title=some long long long ong title,
find y axis rotation=\myangle,title style={rotate=\myangle}]
\addplot3 [
surf,
domain=-2:2,
domain y=-1.3:1.3,
] {exp(-x^2-y^2)*x};
\end{groupplot}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容