PGFPlot/Tikz 图例错误

PGFPlot/Tikz 图例错误

我想为我的两个图(红色和黑色)添加图例,但图例似乎存在一些缩放问题。我认为这可能是由于移位和域的变化造成的,但我不确定!

\newcommand{\vasymptote}[2][]{
\draw [color=gray,densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\begin{tikzpicture}
    \begin{axis}[
        xlabel={Distance (m)},
        ylabel={Moment (kNm)}, xmin=0,xmax=60,width=29.7cm,height=21cm, title={Moment Envelopes}, grid=both, y dir=reverse, ytick={-5500,-5000,...,1500},]

  % Axis
    \addplot[gray,thick,domain=0:60,shift={(0,0)},forget plot] {0}; %x axis
   \vasymptote {7.5};
   \vasymptote {22.5};
   \vasymptote {37.5};
   \vasymptote {52.5};

    % 1.2G + 1.5 Q
  \addplot[black,smooth,domain=0:7.5,shift={(0,0)}, line legend] {-46.61*x^2 - 349.6*x}; % A
    \addplot[black,smooth,domain=0:15,shift={(75,0)}, forget plot] {-54.22*x^2 + 829.29*x - 2206}; % B
  \addplot[black,smooth,domain=0:15,shift={(225,0)}, forget plot] {-54.22*x^2 + 813.22*x - 2020}; % C
  \addplot[black,smooth,domain=0:15,shift={(375,0)}, forget plot] {-54.22*x^2 + 797.16*x - 1965}; % D
  \addplot[black,smooth,domain=0:7.5,shift={(525,0)}, forget plot] {-46.61*(7.5-x)^2 - 349.6*(7.5-x)}; % E

    \addlegendentry{Black Line}

    % G + 0.3 Q
    \addplot[red,smooth,domain=0:7.5,shift={(0,0)}, line legend] {-29.36*x^2 - 220.25*x}; % A
    \addplot[red,smooth,domain=0:15,shift={(75,0)}, forget plot] {-36.6*x^2 + 559.77*x - 1489.05}; % B
    \addplot[red,smooth,domain=0:15,shift={(225,0)}, forget plot] {-36.6*x^2 + 548.92*x - 1363.5}; % C
    \addplot[red,smooth,domain=0:15,shift={(375,0)}, forget plot] {-36.6*x^2 + 538.68*x - 1326.38}; % D
    \addplot[red,smooth,domain=0:7.5,shift={(525,0)}, forget plot] {-29.36*(7.5-x)^2 - 220.25*(7.5-x)}; % E

     \addlegendentry{Red Line}

\end{axis}
\end{tikzpicture}

取消反转 y 轴会导致图例尺寸减小,但不能解决问题。

任何帮助都将非常有帮助,谢谢!

答案1

是的,这是由命令引起的shift。您不应该使用常规的 TikZ 转换命令(如shiftscale)来更改绘图,因为它们在“纸张域”中运行,而您应该在“数据域”中工作。因此,如果您想将绘图向右移动 7.5 个“数据单位”,您可以更改函数(\addplot [...] { -36.6*(x-7.5)^2 + 559.77*(x-7.5) - 1489.05 };),也可以使用坐标过滤器:

x filter/.code=\pgfmathparse{\pgfmathresult+7.5}

将会给图中所有的 x 坐标添加 7.5。

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\newcommand{\vasymptote}[2][]{
\draw [color=gray,densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\begin{tikzpicture}
    \begin{axis}[
        xlabel={Distance (m)},
        ylabel={Moment (kNm)}, xmin=0,xmax=60,width=29.7cm,height=21cm, title={Moment Envelopes}, grid=both, y dir=reverse, ytick={-5500,-5000,...,1500},]

  % Axis
    \addplot[gray,thick,domain=0:60,shift={(0,0)},forget plot] {0}; %x axis
   \vasymptote {7.5};
   \vasymptote {22.5};
   \vasymptote {37.5};
   \vasymptote {52.5};

    % 1.2G + 1.5 Q
  \addplot[black,smooth,domain=0:7.5, line legend] {-46.61*x^2 - 349.6*x}; % A
    \addplot[black,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+7.5}, forget plot] {-54.22*x^2 + 829.29*x - 2206}; % B
  \addplot[black,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+22.5}, forget plot] {-54.22*x^2 + 813.22*x - 2020}; % C
  \addplot[black,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+37.5},, forget plot] {-54.22*x^2 + 797.16*x - 1965}; % D
  \addplot[black,smooth,domain=0:7.5,x filter/.code=\pgfmathparse{\pgfmathresult+52.5}, forget plot] {-46.61*(7.5-x)^2 - 349.6*(7.5-x)}; % E

    \addlegendentry{Black Line}

    % G + 0.3 Q
    \addplot[red,smooth,domain=0:7.5, line legend] {-29.36*x^2 - 220.25*x}; % A
    \addplot[red,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+7.5}, forget plot] {-36.6*x^2 + 559.77*x - 1489.05}; % B
    \addplot[red,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+22.5}, forget plot] {-36.6*x^2 + 548.92*x - 1363.5}; % C
    \addplot[red,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+37.5}, forget plot] {-36.6*x^2 + 538.68*x - 1326.38}; % D
    \addplot[red,smooth,domain=0:7.5, x filter/.code=\pgfmathparse{\pgfmathresult+52.5}, forget plot] {-29.36*(7.5-x)^2 - 220.25*(7.5-x)}; % E

     \addlegendentry{Red Line}

\end{axis}
\end{tikzpicture}
\end{document}

相关内容