tikz pgf 隐藏或显示图例、颜色条、情节线 - 类似于隐藏轴

tikz pgf 隐藏或显示图例、颜色条、情节线 - 类似于隐藏轴

如果您有一个复杂的 tikzpicture,其中包含许多\addplot命令和\addlegendentry命令、对许多数据集的引用、可能还有一个colorbar等等:是否有一种方便的方法可以仅隐藏/显示图例或仅隐藏/显示颜色条,或者隐藏所有情节线同时保留图例?

我想避免取消注释和编辑许多内容,例如,有一种非常方便的方法来隐藏轴,方法是使用hide axis,tikzpicture 的轴选项中的某个位置。我正在寻找类似的命令来隐藏图例或隐藏颜色条或隐藏实际的线数据(图),同时仍显示正确的图例(具有正确的颜色、条目等)。

例如,目标是能够创建一个 pdf,它只显示裁剪的图例(所有正确的线条都具有与图相应的颜色以及正确的图例条目),但隐藏了图形的所有其他元素(颜色条、轴、图线……)。这样的事情可能吗?我想避免手动创建图例,如下所述:54794

答案1

正如我在问题下方的评论中所述,您可以将图例外部化为 PDF。为此,请查看以下示例(包括评论)。

% use this document to externalize the legend of the plot
% (it is important that the `--shell-escape' feature is enabled)
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    % load `external' library from PGFPlots and not TikZ,
    % because its newer and some bugs are fixed
    \usetikzlibrary{
        pgfplots.external,
    }
    % when you are new to LaTeX/PGFPlots then you should use
    % the newest `compat' level to use all new features
    \pgfplotsset{
        compat=newest,
    }
    % activate externalization
    \tikzexternalize[
        % with the following key you can define where to store the
        % externalized files. Otherwise they will be stored in the
        % main/\jobname folder
        prefix=Pics/pgf-export/,
        % only externalize stuff that is "named" with `\tikzsetnextfilename'
        only named=true,
%        % if you have to force to reexternalize stuff, uncomment the next line
%        force remake,
    ]
\begin{document}
% if you (also) want to externalize the plot, give him a name
% (uncomment the next line)
%    \tikzsetnextfilename{linear_plots}
    \begin{tikzpicture}
        \begin{axis}[
            legend columns=-1,
            legend entries={
                $(x+0)^k$;,
                $(x+1)^k$;,
                $(x+2)^k$;,
                $(x+3)^k$
            },
            legend to name=legend:linear_plots,
        ]
            \addplot {x};
            \addplot {x+1};
            \addplot {x+2};
            \addplot {x+3};
        \end{axis}
    \end{tikzpicture}

    bla
    % to externalize the (external) legend, give it a name with the
    % following command
    \tikzsetnextfilename{linear_plots_legend}
    % then reference the legend, so it is drawm and thus externalized
    \ref{legend:linear_plots}
    blub
\end{document}

然后,您只需\includegraphics在真实文档中使用图例即可在任何您想要的位置显示该图例。

% in your "real" document you can then include the externalized plot/legend
\documentclass[border=2mm]{standalone}
\usepackage{graphicx}
    % also add the externalize path (from above)
    % to the `\graphicspath'
    \graphicspath{{Pics/pgf-export/}}
\begin{document}
    bla \includegraphics{linear_plots_legend} blub
\end{document}

相关内容