![清晰地编辑图例和 Y 轴的名称 2](https://linux22.com/image/303303/%E6%B8%85%E6%99%B0%E5%9C%B0%E7%BC%96%E8%BE%91%E5%9B%BE%E4%BE%8B%E5%92%8C%20Y%20%E8%BD%B4%E7%9A%84%E5%90%8D%E7%A7%B0%202.png)
我希望我的图例仅显示以下内容:用填充黑色标记 BHI 增长,用填充白色标记 BHI ph 变化。此外,Y 轴 2 的名称必须正确,但具体来说,我的图表名称为 Y 轴 2,Y 轴 1。此外,线 %f2 从 Y 轴 1 开始
\documentclass{article}
\usepackage{pgfplots,tikz}
\usepackage{caption}
\DeclareCaptionLabelSeparator{fullstop}{.\quad}
\captionsetup[figure]{labelsep=fullstop}
\pgfplotsset{width=120mm}
\begin{document}
\begin{figure}
\begin{tikzpicture}
% f1
\begin{axis}[
color=black,
scale only axis,
xmin=0,
xmax=168,
%xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,48,72,168},
xtick={0,12,24,48,72,168}, %%%<------------- here
xlabel=\ {Time},
ymin=0,
ymax=10,
ytick={0,1,2,3,4,5,6,7,8,9,10},% your code
yticklabels={0,1,2,3,4,5,6,7,8,9,10},% your code
ylabel=\ {log(Cfu/cm$^{2}$)},
y axis line style={black},
ytick style={black},
yticklabel style={black},
axis x line*=bottom,
axis y line*=left,
tick align = outside,]
\addplot [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=square*,
mark options={solid,fill=white}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=*,
mark options={solid,fill=white}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=diamond*,
mark options={solid,fill=white}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=triangle*,
mark options={solid,fill=white}]
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\\};
\legend {BHI growth, BHI ph change}
\end{axis}
% f2
\begin{axis}[
color=black,
scale only axis,
xmin=0,
xmax=168,
ymin=0,
ymax=6,
ytick={0,1,2,3,4,5,6},
yticklabels={0,1,2,3,4,5,6},
axis x line*=none,
axis y line*=right,
ylabel=\ {Relative Expression}
ylabel style=black,
hide x axis,
tick align =outside,]
\addplot [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=square*,
mark options={solid,fill=black}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=*,
mark options={solid,fill=black}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=diamond*,
mark options={solid,fill=black}]
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\\};
\addplot [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=triangle*,
mark options={solid,fill=black}]
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\\};
\legend {BHI growth, BHI ph change}
\end{axis}
\end{tikzpicture}
\caption{Growth of different types foodborne pathogens.}
\end{figure}
\end{document}
答案1
假设同一图中两个不同的坐标大概一个坏主意这是对你的问题的部分解决方案。
我尝试将您的代码简化为一个更简单的示例。添加\pgfplotsset{compat=newest}
到序言并更改\begin[axis]
选项可以解决 y 标签的位置问题。
我得到了以下提示关于重叠传说的答案和关于两个(或三个)纵坐标图的答案(这确实是有帮助的,即使你不这么认为)。
说实话,我不明白你对这个图例有何疑问。
\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}$) (red)},
axis y line*=left,
ymin=0, ymax=10,
legend pos=south west]
\addplot [red, mark=*]
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\\};
\label{plot_one}
\addlegendentry{plot 1}
\end{axis}
% f2
\begin{axis}[scale only axis,
hide x axis,
xmin=0, xmax=168,
ylabel={Relative Expression (blue)},
axis y line*=right,
ymin=0, ymax=6,
legend pos=south west]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addplot [blue, mark=*]
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 2}
\end{axis}
\end{tikzpicture}
\end{document}