2

I am trying to customize the marks of a plot (using Tikzpicture), so that, among other things, they are not made of a given color but take the color of the line.

I have thus created a cusomized mark, called mymarks, using \pgfdeclareplotmark

\pgfdeclareplotmark{mymarks}{
    \color{red}
    \pgfpathcircle{\pgfpointorigin}{4pt}\pgfusepath{fill}
    \color{white}
    \pgfpathcircle{\pgfpointorigin}{2pt}\pgfusepath{fill}
}

For the moment, due to the declaration \color{red}, my marks have the perfect shape but are always red. Yet, I would like to have them of the color of the curve they are put on. In the following screenshot, for instance, I would like them to be purple on the purple curve, yellow on the yellow curve and blue on the blue curve. I would thus like to use something like \color{\pgfcolor} (by analogy to the \pgfpointorigin that pass the coordinates of the points) but that does not work.

Do you have any idea how to do that?

Here is a reduced example of what I coded:

\documentclass[12pt,a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfdeclareplotmark{mymarks}{
    \color{red}
    \pgfpathcircle{\pgfpointorigin}{4pt}\pgfusepath{fill}
    \color{white}
    \pgfpathcircle{\pgfpointorigin}{2pt}\pgfusepath{fill}
}


\begin{document}

\begin{tikzpicture}[xscale=1, yscale=1]
        \draw[->] (-0.5, 0) -- (11,0);
        \draw[->] (0, -0.5) -- (0,11);
        \draw [color=blue, line width=2pt] plot [mark = mymarks] coordinates {(0,0) (1,1) (2,2) (3,5) (4,8) (5,2) (6,9) (7,5) (8,4) (9,3) (10,1)};
        \draw [color=yellow, line width=2pt] plot [mark = mymarks] coordinates {(0,8) (1,7) (2,6) (3,4) (4,8) (5,7) (6,8) (7,9) (8,3) (9,1) (10,3)};
        \draw [color=green, line width=2pt] plot [mark = mymarks] coordinates {(0,4) (1,4) (2,4) (3,8) (4,3) (5,5) (6,3) (7,2) (8,7) (9,8) (10,7)};
    \end{tikzpicture}

\end{document}

With this, I get the following result:

enter image description here

What I would like to get is this:

enter image description here

(I did that manually by creating mymarksblue, mymarksyellow and mymarksgreen, but I would like to avoid creating one type of mark for every color I have in my actual document, as there are many of them)

Thank you so much!

Simon

4
  • Welcome! It would be nice if you could add a complete, small compilable example so that people can play with possible solutions without having to guess it.
    – Rmano
    Jan 31, 2022 at 22:04
  • Hi Rmano, thank you for your reply. I have just updated my question accordingly, now it includes a reduced example.
    – Simon
    Feb 1, 2022 at 6:47
  • Why fill the circles and not stroke them? I guess there will be more to your plotmark, as a circle already exist! I do not know, but I think you need a trick like this: tex.stackexchange.com/a/219088/8650 to retrieve the stoke color. Feb 1, 2022 at 7:53
  • Yes, it would work with stroking as well. The problem is that it I set stroke color to white and stroke my points (with \pgfsetstrokecolor{white} \pgfpathcircle{\pgfpointorigin}{3pt} \pgfusepath{fill}) , I get an inverted result: the "outside" of the point is white, and the inside is of the point is of the color of the curve.
    – Simon
    Feb 1, 2022 at 8:55

1 Answer 1

1

On working on something completely different, I discovered the pgfscope environment, that saved me on this one. Any change that is set in the pgfscope environment is limited to the environment, hence I can easily change the color to white within the environment after drawing each point:

\documentclass[12pt,a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfdeclareplotmark{mymarks}{
    \pgfpathcircle{\pgfpointorigin}{4pt}\pgfusepath{fill}
    \begin{pgfscope}
        \color{white}
        \pgfpathcircle{\pgfpointorigin}{2pt}\pgfusepath{fill}
    \end{pgfscope}
}

\begin{document}

\begin{tikzpicture}[xscale=1, yscale=1]
        \draw[->] (-0.5, 0) -- (11,0);
        \draw[->] (0, -0.5) -- (0,11);
        \draw [color=blue, line width=2pt] plot [mark = mymarks] coordinates {(0,0) (1,1) (2,2) (3,5) (4,8) (5,2) (6,9) (7,5) (8,4) (9,3) (10,1)};
        \draw [color=yellow, line width=2pt] plot [mark = mymarks] coordinates {(0,8) (1,7) (2,6) (3,4) (4,8) (5,7) (6,8) (7,9) (8,3) (9,1) (10,3)};
        \draw [color=green, line width=2pt] plot [mark = mymarks] coordinates {(0,4) (1,4) (2,4) (3,8) (4,3) (5,5) (6,3) (7,2) (8,7) (9,8) (10,7)};
    \end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .