1

I'm trying to do a plot using pgfplots from .cvs data. I want to use 2 y-axis and change the color of one line. Here is the code:

 \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}

And the result is enter image description here

  1. Why I'm not getting the red square mark?
  2. Why my right y axis legend is in the left?
1

Use axis y line=right (not axis y line*=right), and apply the [mark=square, red] option to \addplot:

enter image description here

Code:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.