使用表格和 2 个 y 轴的 Pgfplots

使用表格和 2 个 y 轴的 Pgfplots

我正在尝试使用 pgfplots 从 .cvs 数据中绘制图表。我想使用 2 个 y 轴并更改一条线的颜色。以下是代码:

 \documentclass[12pt,a4paper]{article}%
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{makeidx}
    \usepackage{graphicx}
    \usepackage[brazil]{babel}
    \usepackage{tikz} 
    \usepackage{filecontents}

    \begin{document}

    \begin{filecontents*}{mydata.csv}
    Dosagem,    Volume, Massa de CAP,       CYN,    rem
    0,          500,    0.0,                172.503,0   
    5,          500,    2.5,                56.599, 67.190
    10,         500,    5.0,                37.559, 78.227  
    15,         500,    7.5,                15.799, 90.841  
    20,         500,    10.0,               27.649, 83.972  
    30,         500,    15.0,               9.204,  94.664  
    40,         500,    20.0,               5.048,  97.074
    50,         500,    25.0,               3.541,  97.947
    \end{filecontents*}

    \begin{tikzpicture}
    \pgfplotsset{set layers}
    \begin{axis}[
scale only axis,
axis y line*=left,
xmin=0, xmax=50, ymax=180, ymin=0,
xlabel={Dosagem de CAP, $mg/L$},
xtick=data, xmajorgrids,
ytick={0,20,...,180},
tick align=outside,
minor y tick num=5,
ylabel={Conncentração de CYN, $\mu g/L$}]
    \addplot table[x=Dosagem, y=CYN, col sep=comma,] {mydata.csv};
    \end{axis}

    \begin{axis}[
scale only axis,
xmin=0, xmax=50,
axis y line*=right,
ymin=0, ymax=100,
ylabel=Remoção,
ytick={0,10,...,100},
tick align=outside]
    \addplot table[x=Dosagem, y=rem, col sep=comma, smooth, mark=square, red] {mydata.csv};
    \end{axis}
    \end{tikzpicture}

    \end{document}

结果是 在此处输入图片描述

  1. 为什么我没有得到红色方形标记?
  2. 为什么我的右侧 y 轴图例在左侧?

答案1

使用axis y line=right(不是axis y line*=right),并将[mark=square, red]选项应用于\addplot

在此处输入图片描述

代码:

\documentclass[12pt,a4paper]{article}%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{makeidx}
\usepackage{pgfplots}
\usepackage{tikz} 
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{mydata.csv}
Dosagem,    Volume, Massa de CAP,       CYN,    rem
0,          500,    0.0,                172.503,0   
5,          500,    2.5,                56.599, 67.190
10,         500,    5.0,                37.559, 78.227  
15,         500,    7.5,                15.799, 90.841  
20,         500,    10.0,               27.649, 83.972  
30,         500,    15.0,               9.204,  94.664  
40,         500,    20.0,               5.048,  97.074
50,         500,    25.0,               3.541,  97.947
\end{filecontents*}

\begin{tikzpicture}
%%\pgfplotsset{set layers}
\begin{axis}[
    scale only axis,
    axis y line*=left,
    xmin=0, xmax=50, ymax=180, ymin=0,
    xlabel={Dosagem de CAP, $mg/L$},
    xtick=data, xmajorgrids,
    ytick={0,20,...,180},
    tick align=outside,
    minor y tick num=5,
    ylabel={Conncentração de CYN, $\mu g/L$}]
\addplot table[x=Dosagem, y=CYN, col sep=comma] {mydata.csv};
\end{axis}

\begin{axis}[
    scale only axis,
    xmin=0, xmax=50,
    axis y line=right, %% <--- Removed asterix
    ymin=0, ymax=100,
    ylabel=Remoção,
    ytick={0,10,...,100},
    tick align=outside]
\addplot[mark=square, red] %% <-- Apply options to `\addplot`
    table[x=Dosagem, y=rem, col sep=comma, smooth] {mydata.csv}; 
\end{axis}
\end{tikzpicture}

\end{document}

相关内容