PGFlots 的“y”选项无法与多线图中的 PGFplotsTableRead 配合使用

PGFlots 的“y”选项无法与多线图中的 PGFplotsTableRead 配合使用

我有一个非常简单的折线图,我想用 制作PGFplotstableread。当我画线时,似乎只有第一个 Y 被识别。

\documentclass[11pt, twoside, a4paper]{report}
\usepackage{caption}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16,
    % created a style for the common `axis' options
    my axis style/.style={
        width=\linewidth,
        height=0.35\linewidth,
        bar width=0.9,
        enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
        ymin=0,
        legend style={
            at={(0.5,1.05)},    % <-- adapted
            anchor=south,       % <-- changed from `north'
            legend columns=2,
        },
        ylabel={PR\textsubscript{A}},
        xtick=data,
        axis lines*=left,
        ymajorgrids,
        %
        table/x=x,
    },}

\pgfplotstableread{%
        x   Floating_cSi_inner  Floating_cSi_middle Floating_cSi_outer  Reference_cSi_average   Floating_cSi_average
        20  27.7    28.3    26.4    25.7    27.5
        21  35.9    35.8    34.1    33.2    35.3
        22  35.7    34.5    31.5    35.6    33.9
        23  30.5    28.6    25.0    31.0    28.0
        24  30.9    29.3    25.9    29.6    28.7
        25  31.8    30.3    25.8    28.7    29.3
        26  38.2    37.6    34.5    34.4    36.7
        27  39.5    38.2    34.3    36.3    37.3
        28  35.5    34.7    29.9    34.7    33.3
        29  39.3    37.6    32.1    36.9    36.3
        }{\loadedtableweeklywatsp}

\begin{document}

\begin{figure}[h]
    \begin{tikzpicture}
    \begin{axis}[my axis style, ylabel = WAT (\degree C), xlabel = Week number]
    \addplot [ultra thick, draw=red!50!black, smooth, y = Floating_cSi_inner] table {\loadedtableweeklywatsp};
    \addplot [ultra thick, draw=blue!50!black, smooth, y = Floating_cSi_middle] table {\loadedtableweeklywatsp};
    \addplot [ultra thick, draw=green!50!black, smooth, y = Floating_cSi_outer] table {\loadedtableweeklywatsp};
    \end{axis}
    \end{tikzpicture}
    \label{fig:WAT_SP_float}
    \caption{Weighted average temperatures for SunProjects floating cSi system }
\end{figure}

\end{document}

输出(应为三行):

在此处输入图片描述

答案1

这是一个相当常见的(用户)错误。正如在在问题下方评论 y=<column name>它不是选项的参数\addplot,而是选项的参数table

因此,简单移动这些选项即可解决您的问题。

  • 错误的:\addplot [...,y=<column name>] table {...};
  • 正确的:\addplot [...] table [y=<column name>] {...};

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.16,
        % created a style for the common `axis' options
        % (this style was already stated at the answer
        %  <https://tex.stackexchange.com/a/442872/95441>)
        my axis style/.style={
            width=\linewidth,
            height=0.35\linewidth,
            bar width=0.9,
            enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
            ymin=0,
            legend style={
                at={(0.5,1.05)},    % <-- adapted
                anchor=south,       % <-- changed from `north'
                legend columns=2,
            },
            ylabel={PR\textsubscript{A}},
            xtick=data,
            axis lines*=left,
            ymajorgrids,
            %
            table/x=x,
        },
    }
    \pgfplotstableread{
        x   Floating_cSi_inner  Floating_cSi_middle Floating_cSi_outer  Reference_cSi_average   Floating_cSi_average
        20  27.7    28.3    26.4    25.7    27.5
        21  35.9    35.8    34.1    33.2    35.3
        22  35.7    34.5    31.5    35.6    33.9
        23  30.5    28.6    25.0    31.0    28.0
        24  30.9    29.3    25.9    29.6    28.7
        25  31.8    30.3    25.8    28.7    29.3
        26  38.2    37.6    34.5    34.4    36.7
        27  39.5    38.2    34.3    36.3    37.3
        28  35.5    34.7    29.9    34.7    33.3
        29  39.3    37.6    32.1    36.9    36.3
    }{\loadedtableweeklywatsp}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        my axis style,
        ylabel=WAT (\si{\celsius}),
        xlabel=Week number,
        % (moved common option here. You could alternatively move it to the `my axis style`)
        smooth,
    ]
        % moved `y=<column name> from the `\addplot' options to the `table' options
        \addplot [ultra thick,draw=red!50!black] table [y = Floating_cSi_inner] {\loadedtableweeklywatsp};
        \addplot [ultra thick,draw=blue!50!black] table [y = Floating_cSi_middle] {\loadedtableweeklywatsp};
        \addplot [ultra thick,draw=green!50!black] table [y = Floating_cSi_outer] {\loadedtableweeklywatsp};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容