我想在图表下方给出图例和 \caption{不同类型的食源性病原体的增长。}。删除线 papralel x 轴
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\begin{document}
\begin{tikzpicture}
% f1
\begin{axis}[scale only axis,
xlabel=Time,
xmin=0, xmax=168,
ylabel={log(Cfu/cm$^{2}$)},
axis y line*=left,
ymin=0, ymax=10,
legend pos=south west]
\addplot [black, fill=white, mark=square*]
table[row sep=crcr]{
0 9\\
4 9.1\\
8 9.8\\
12 9.8\\
24 9.162\\
48 9\\
72 9.1\\
168 9.8\\};
\addplot [black, fill=white, mark=*]
table[row sep=crcr]{
0 9.2\\
4 8.1\\
8 8.8\\
12 8.8\\
24 9.62\\
48 9.3\\
72 9.1\\
168 8.8\\};
\addplot [black, fill=white, mark=diamond*]
table[row sep=crcr]{
0 8.7\\
4 9.2\\
8 8.9\\
12 9.6\\
24 9.2\\
48 8\\
72 8.1\\
168 8.8\\};
\addplot [black, fill=white, mark=triangle*]
table[row sep=crcr]{
0 7\\
4 7.1\\
8 7.8\\
12 7.8\\
24 7.162\\
48 7.6\\
72 7.1\\
168 7.8\\};
\label{plot_one}
\addlegendentry{plot 1}
\end{axis}
% f2
\begin{axis}[scale only axis,
hide x axis,
xmin=0, xmax=168,
ylabel={Relative Expression},
axis y line*=right,
ymin=0, ymax=6,
legend pos=south west]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 4}
\addplot [black, mark=square*]
table[row sep=crcr]{
0 1.3\\
4 1.8\\
8 1.4\\
12 1.3\\
24 1.85\\
48 1.48\\
72 1.63\\
168 1.75\\};
\addplot [black, mark=*]
table[row sep=crcr]{
0 1.6\\
4 3.8\\
8 2.4\\
12 1.3\\
24 2.85\\
48 1.48\\
72 2.63\\
168 3.75\\};
\addplot [black, mark=diamond*]
table[row sep=crcr]{
0 0.3\\
4 0.8\\
8 0.4\\
12 0.3\\
24 0.85\\
48 0.48\\
72 0.63\\
168 0.75\\};
\addplot [black, mark=triangle*]
table[row sep=crcr]{
0 0.8\\
4 1.2\\
8 1.24\\
12 2.3\\
24 2.85\\
48 2.48\\
72 2.63\\
168 2.75\\};
\addlegendentry{plot 2}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
因为caption
您需要将图表放入figure
环境中。要在 中查看它,应添加preview
选项。对于图表外的图例,仅假设选项,因此对于下面的图例,您需要更改图例样式。floats
pgfplot
outer north east
我不明白你喜欢在图例中放什么,所以我保留了f1
你在 MWE 中提供的图例,重新设计了它,因为f2
我认为图例应该是这样的。我在你的 MWE 中的更改用% <----
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{caption} % <----
\captionsetup{font = small} % <----
\usepackage[active,floats,tightpage]{preview}% <---
\setlength\PreviewBorder{1em}
\begin{document}
%---------------------------------------------------------------%
\begin{figure} % <---
\centering % <---
\begin{tikzpicture}
%%%% f1
\begin{axis}[scale only axis,
xlabel=Time,
xmin=0, xmax=168,
ylabel={log(Cfu/cm$^{2}$)},
axis y line*=left,
ymin=0, ymax=10,
legend style={at={(0.48,-0.15)}, % <---
anchor=north east}] % <---
\addplot [black, fill=white, mark=square*]
table[row sep=crcr]{
0 9\\
4 9.1\\
8 9.8\\
12 9.8\\
24 9.162\\
48 9\\
72 9.1\\
168 9.8\\};
\addplot [black, fill=white, mark=*]
table[row sep=crcr]{
0 9.2\\
4 8.1\\
8 8.8\\
12 8.8\\
24 9.62\\
48 9.3\\
72 9.1\\
168 8.8\\};
\addplot [black, fill=white, mark=diamond*]
table[row sep=crcr]{
0 8.7\\
4 9.2\\
8 8.9\\
12 9.6\\
24 9.2\\
48 8\\
72 8.1\\
168 8.8\\};
\addplot [black, fill=white, mark=triangle*]
table[row sep=crcr]{
0 7\\
4 7.1\\
8 7.8\\
12 7.8\\
24 7.162\\
48 7.6\\
72 7.1\\
168 7.8\\};
\label{plot_one}
\addlegendentry{plot 1}
\end{axis}
%%%% f2
\begin{axis}[scale only axis,
hide x axis,
xmin=0, xmax=168,
ylabel={Relative Expression},
axis y line*=right,
ymin=0, ymax=6,
legend columns=2, % <---
legend style={at={(0.52,-0.15)}, % <---
anchor=north west}]% <---
]
% \addlegendimage{/pgfplots/refstyle=plot_one}
\addplot [black, mark=square*]
table[row sep=crcr]{
0 1.3\\
4 1.8\\
8 1.4\\
12 1.3\\
24 1.85\\
48 1.48\\
72 1.63\\
168 1.75\\};
\addlegendentry{plot 1} % <---
\addplot [black, mark=*]
table[row sep=crcr]{
0 1.6\\
4 3.8\\
8 2.4\\
12 1.3\\
24 2.85\\
48 1.48\\
72 2.63\\
168 3.75\\};
\addlegendentry{plot 2} % <---
\addplot [black, mark=diamond*]
table[row sep=crcr]{
0 0.3\\
4 0.8\\
8 0.4\\
12 0.3\\
24 0.85\\
48 0.48\\
72 0.63\\
168 0.75\\};
\addlegendentry{plot 3} % <---
\addplot [black, mark=triangle*]
table[row sep=crcr]{
0 0.8\\
4 1.2\\
8 1.24\\
12 2.3\\
24 2.85\\
48 2.48\\
72 2.63\\
168 2.75\\};
\label{plot_four}
\addlegendentry{plot 4} % <---
\end{axis}
\end{tikzpicture}
\caption{Growth of different types foodborne pathogens.} % <---
\end{figure} % <---
\end{document}