当我改变颜色时,pgfplots 绘图标记会消失

当我改变颜色时,pgfplots 绘图标记会消失

我创建了两个重叠的图(感谢使用 pgfplots 在一个图中绘制曲线和多个 y 轴):

在此处输入图片描述

当我在第二个中改变颜色时,情节标记消失了:

在此处输入图片描述

这是我的 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}

(我知道部分格式代码是被黑客入侵并且不够优雅,但我不想问我能回答的问题,无论回答得多么粗糙。)

答案1

发生这种情况是因为您正在使用\addplot [<options>],这会关闭指定绘图标记的绘图循环列表。有两种方法可以解决此问题:

您可以使用\addplot +[red, mark options={fill=red}]+保持绘图循环列表处于活动状态,并仅附加方括号中给出的选项。

或者您可以使用\addplot [red, mark=*],从而设置所有必需的设置,而不依赖于绘图循环列表。

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

相关内容