3

I have created two overlapping plots (thanks to Drawing curves and multiple y-axes in one plot with pgfplots):

enter image description here

When I change the color in the second one, the plot marks vanish:

enter image description here

Here's my MWE:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

%deal with warning message in log
\pgfplotsset{compat=1.8}

\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\begin{document}

\pgfplotstableread[col sep=comma]{
year, number, cost
2006, 8.6, 103
2007, 7.1, 118
2008, 5.3, 75 
2009, 3.2, 52 
2010, 3.5, 62 
2011, 4.3, 103
2012, 5.8, 116
}\pennies

\newcommand{\mywidth}{10cm}
\newcommand{\blueheight}{5cm}
\newcommand{\redheight}{6cm}

\begin{tikzpicture}[font=\sffamily\sansmath]
% number, blue
\begin{axis}[ 
      title={Many Pennies}, 
      width=\mywidth, height=\blueheight, 
      ymin=0,
      axis y line*=left,
      ylabel={\color{blue}number minted},
      yticklabel style={color=blue},
      yticklabels={,0b,2b,4b,6b,8b}, % why do I need empty first label?
      axis x line*=bottom,
      xlabel={year},
      xtick=data,
      xticklabel style={align=center},
      xticklabels from table={\pennies}{year},
      nodes near coords align={anchor=south,yshift=-6mm},
      nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}b},
    ]
  \addplot table [x expr=\coordindex, y=number]{\pennies};
\end{axis} 
% cost, red
\begin{axis}[ 
      width=\mywidth, height=\redheight, 
      ymin=0,
      axis y line*=right,
      yticklabels={,\$0m, \$50m, \$100m},
      ylabel={\color{red}production cost},
      yticklabel style={color=red},
      hide x axis,
      nodes near coords={\color{red}\$\pgfmathprintnumber{\pgfplotspointmeta}m},
    ]
%  \addplot table [x expr=\coordindex, y=cost]{\pennies};
  \addplot [color=red] table [x expr=\coordindex, y=cost]{\pennies};
\end{axis}
\end{tikzpicture}
\end{document}

(I know that some of the formatting code is hacked and inelegant, but I didn't want to ask questions I managed to answer, however crudely.)

Ethan Bolker
  • 9,333
  • 3
  • 42
  • 69

1 Answers1

5

This happens because you're using \addplot [<options>], which switches off the plot cycle list that specified the plot marks. Two ways to fix this:

You can use \addplot +[red, mark options={fill=red}]. The + keeps the plot cycle list active and merely appends the options given in the square brackets.

Or you can use \addplot [red, mark=*], thereby setting all the required settings without relying on the plot cycle list.

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

%deal with warning message in log
\pgfplotsset{compat=1.8}

\usepackage{helvet}
\usepackage{sansmath}

\begin{document}

\pgfplotstableread[col sep=comma]{
year, number, cost
2006, 8.6, 103
2007, 7.1, 118
2008, 5.3, 75 
2009, 3.2, 52 
2010, 3.5, 62 
2011, 4.3, 103
2012, 5.8, 116
}\pennies

\newcommand{\mywidth}{10cm}
\newcommand{\blueheight}{5cm}
\newcommand{\redheight}{6cm}

\begin{tikzpicture}[font=\sffamily\sansmath]
% number, blue
\begin{axis}[ 
      title={Many Pennies}, 
      width=\mywidth, height=\blueheight, 
      ymin=0,
      axis y line*=left,
      ylabel={\color{blue}number minted},
      yticklabel style={color=blue},
      yticklabels={,0b,2b,4b,6b,8b}, % why do I need empty first label?
      axis x line*=bottom,
      xlabel={year},
      xtick=data,
      xticklabel style={align=center},
      xticklabels from table={\pennies}{year},
      nodes near coords align={anchor=south,yshift=-6mm},
      nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}b},
    ]
  \addplot table [x expr=\coordindex, y=number]{\pennies};
\end{axis} 
% cost, red
\begin{axis}[ 
      width=\mywidth, height=\redheight, 
      ymin=0,
      axis y line*=right,
      yticklabels={,\$0m, \$50m, \$100m},
      ylabel={production cost},
      ylabel style=red,
      yticklabel style={red},
      hide x axis,
      nodes near coords={\$\pgfmathprintnumber{\pgfplotspointmeta}m},
    ]
%  \addplot table [x expr=\coordindex, y=cost]{\pennies};
  \addplot [red, mark=*] table [x expr=\coordindex, y=cost]{\pennies};
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450