我需要将下面代码中的两个 tikz 图像外部化为 PDF(用于期刊文章),然后读取这些 PDF,以及使用从外部为它们添加单个图例\pgfplotslegendfromname{named}
。 外部化 tikz 图像很容易,但引用第一个 tikz 中定义的图例设置似乎很复杂。
另一种方法是生成完整图形的 PDF,然后修剪它,但我想避免这样做。
有人可以帮我吗?
\begin{figure}
\centering
\begin{subfigure}[t]{0.495\textwidth}
\centering
\begin{tikzpicture}
\begin{loglogaxis}[%
scale=0.9,
xlabel={A},
xmajorgrids,
ylabel={B},
ymajorgrids,
axis background/.style={fill=white},
legend style={text=black, legend cell align=left,legend columns=3,/tikz/every even column/.append style={column sep=0.5cm}},
legend to name=named]
\addplot [color=black,loosely dashed,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{1.txt};
\addplot [color=black,loosely dotted,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{2.txt};
\addplot [color=black,loosely dashdotted,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{3.txt};
\addplot [color=black,dashed,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{4.txt};
\addplot [color=black,dotted,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{5.txt};
\addplot [color=black,dashdotted,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{6.txt};
\addplot [color=black,densely dashed,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{7.txt};
\addplot [color=black,densely dotted,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{8.txt};
\addplot [color=black,densely dashdotted,line width=0.5pt] table [col sep=comma,x index=0,y index=1]{9.txt};
\addlegendentry{$10\%$}
\addlegendentry{$20\%$}
\addlegendentry{$30\%$}
\addlegendentry{$40\%$}
\addlegendentry{$50\%$}
\addlegendentry{$60\%$}
\addlegendentry{$70\%$}
\addlegendentry{$80\%$}
\addlegendentry{$90\%$}
\end{loglogaxis}
\end{tikzpicture}
\caption{Case 1\label{fig:6a}}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.495\textwidth}
\centering
\begin{tikzpicture}
\begin{loglogaxis}[%
scale=0.9,
xlabel={A},
xmajorgrids,
ylabel={B},
ymajorgrids,
axis background/.style={fill=white}]
\addplot [color=black,loosely dashed,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{1.txt};
\addplot [color=black,loosely dotted,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{2.txt};
\addplot [color=black,loosely dashdotted,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{3.txt};
\addplot [color=black,dashed,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{4.txt};
\addplot [color=black,dotted,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{5.txt};
\addplot [color=black,dashdotted,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{6.txt};
\addplot [color=black,densely dashed,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{7.txt};
\addplot [color=black,densely dotted,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{8.txt};
\addplot [color=black,densely dashdotted,line width=0.5pt] table [col sep=comma,x index=0,y index=13]{9.txt};
\end{loglogaxis}
\end{tikzpicture}
\caption{Case 2\label{fig:6b}}
\end{subfigure}
\vspace{0.5cm}
\\
\pgfplotslegendfromname{named}
\end{figure}
答案1
对我来说,这非常好。
请注意,我已经大大简化了您的代码(无论如何,这对我们来说是不可编译的,因为我们没有您的表格文件),但应该展示它如何适用于您的代码的原理。
% used PGFPlots v1.16
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{
% use the one from PGFPlots which is more recent than the one from tikz
pgfplots.external,
}
% put all externalized images in a "special" folder
\tikzsetexternalprefix{Pics/pgf-export/}
% enable externalization
\tikzexternalize
\begin{document}
% (I prefer to give the images reasonable names)
\tikzsetnextfilename{first}
\begin{tikzpicture}
\begin{loglogaxis}[
xlabel={A},
ylabel={B},
legend to name=named,
]
\addplot+ [domain=1:10] {x};
\addlegendentry{$10\%$}
\end{loglogaxis}
\end{tikzpicture}
\tikzsetnextfilename{second}
\begin{tikzpicture}
\begin{loglogaxis}[
xlabel={A},
ylabel={B},
]
\addplot+ [domain=1:10] {1/x};
\end{loglogaxis}
\end{tikzpicture}
% (you can also name this image, if you want to)
\tikzsetnextfilename{first-legend}
\pgfplotslegendfromname{named}
\end{document}