2

In the following figure, the are three points each of them has common options \addplot like fill=blue, mark=*, thick, only marks, mark size=4pt, point meta=explicit symbolic, and have some options that distinguish each point like color=blue. When I try to edit some common options I have to repeat the edit for every point because of the code duplication.

Is there a way which enables me to store the common options in a variable so that the duplication is eliminated. Something like that \addplot [unique1=value1, \commonFeaturesVariable] { ...

I found that I can make a global default settings but I don't want that because it will corrupt the figures in the same document that depend on the original default options.

enter image description here

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line = bottom,
    axis y line = left,
    scaled y ticks = false,
    enlarge x limits=0.1,
    nodes near coords,
]
    \addplot[color=blue, fill=blue, mark=*, thick, only marks, mark size=4pt, point meta=explicit symbolic]
        coordinates {
        (1, 4)
        };

    \addplot[color=red, fill=red, mark=*, thick, only marks, mark size=4pt, point meta=explicit symbolic]
        coordinates {
            (2, 9)
            };

    \addplot[color=brown, fill=brown, mark=*, thick, only marks, mark size=4pt, point meta=explicit symbolic]
        coordinates {
            (3, 7)
            };
\end{axis}
\end{tikzpicture}
\end{document}
3

When you use PGF/TikZ/pgfplots you have already loaded the pgfkeys package. You can use it to define a style:

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{%
  myaddplot/.style = {%
    mark=*,
    only marks,
    mark size=4pt,
    point meta=explicit symbolic,
  }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line = bottom,
    axis y line = left,
    scaled y ticks = false,
    enlarge x limits=0.1,
    nodes near coords,
]
    \addplot[color=blue, fill=blue, myaddplot]
        coordinates {
        (1, 4)
        };

    \addplot[color=red, fill=red, myaddplot]
        coordinates {
            (2, 9)
            };

    \addplot[color=brown, fill=brown, myaddplot]
        coordinates {
            (3, 7)
            };
\end{axis}
\end{tikzpicture}
\end{document}

The above will define a global style myaddplot which can be used after it was defined.

As Mr. marmot said in the comments, you can omit fill=<color> here if you will always want the draw and fill color the same because you specify the mark = *. Also, if you will use the same fill and color, then thick will also have no effect because you are plotting only marks.

If you want different colors, though, you can make your style take arguments:

\pgfplotsset{%
  myaddplot/.style args = {#1/#2}{%
    mark=*,
    only marks,
    mark size=4pt,
    point meta=explicit symbolic,
    color=#1,
    fill=#2,
  }
}

then use as:

\addplot[myaddplot=blue/blue]
    coordinates {
    (1, 4)
    };
  • Yes, you are right. I did not zoom in enough to see that. Sorry! – user121799 Sep 4 '18 at 0:31
  • @marmot No problem ;) – Phelype Oleinik Sep 4 '18 at 10:37

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.