benutzerdefinierter Marker (pgfdeclareplotmark) mit Farbe als Argument

benutzerdefinierter Marker (pgfdeclareplotmark) mit Farbe als Argument

Ich versuche, die Form und Farbe eines Markers für ein Streudiagramm anzupassen. Alles funktioniert, wenn ich die Farbe fest codiere (z. B. rot):

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}


\newcommand{\myStyle}{%
% define marker shape here
    \tikzset{marker/.style={circle, inner sep=1.5pt, fill=red}}
}

\pgfdeclareplotmark{mymarker}{%
    \node[marker] {};
}

\begin{document}

\begin{tikzpicture}

\myStyle

\begin{axis}[]

\addplot[only marks, mark=mymarker, smooth] coordinates {
    (3, 0.5)
    (4, 0.6)
    (5, 0.4)
};

\end{axis}
\end{tikzpicture}

Ich habe jedoch Probleme, die Farbe für jedes Diagramm individuell zu gestalten. Ich kann definieren

\newcommand{\myStyle}[1]{%
% define marker shape here
    \tikzset{marker/.style={circle, inner sep=1.5pt, fill=#1}}
}

aber ich denke, ich muss die Farbe innerhalb des Blocks angeben

\pgfdeclareplotmark{mymarker}{%
        \node[marker] {};
    }

um die Farbe für jeden Addplot-Befehl zu ändern. Ich bin mir jedoch nicht sicher, wie ich die Farbe als Argument an den Befehl \pgfdeclareplotmark übergeben kann.

Antwort1

Bearbeiten 1

Bildbeschreibung hier eingeben

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}

\pgfplotsset{compat=1.17} % Ensure compatibility with pgfplots

% Declare macro to create customized markers with a specific color
\newcommand{\declareCustomMarker}[2]{%
    \pgfdeclareplotmark{#1}{%
        \node[circle, inner sep=1.5pt, fill=#2] {};
    }
}

\begin{document}

\begin{tikzpicture}

\begin{axis}[]

% Declare markers with different colors
\declareCustomMarker{mymarker-blue}{blue}
\declareCustomMarker{mymarker-green}{green}

% Use the blue marker for this plot
\addplot[only marks, mark=mymarker-blue, smooth] coordinates {
    (3, 0.5)
    (4, 0.6)
    (5, 0.4)
};

% Use the green marker for this plot
\addplot[only marks, mark=mymarker-green, smooth] coordinates {
    (2, 0.7)
    (4, 0.8)
    (6, 0.9)
};

\end{axis}
\end{tikzpicture}

\end{document}

so was ?

Bildbeschreibung hier eingeben

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}

\pgfkeys{/tikz/.cd,
    marker color/.store in=\markercolor,
    marker color=red % default color
}

\newcommand{\myStyle}[1]{%
    \pgfkeys{/tikz/marker color=#1} % set the marker color
    % define marker shape here
    \tikzset{marker/.style={circle, inner sep=1.5pt, fill=\markercolor}}
}

\pgfdeclareplotmark{mymarker}{%
    \node[marker] {};
}

\begin{document}

\begin{tikzpicture}

\myStyle{red} % <= marker color here

\begin{axis}[]

\addplot[only marks, mark=mymarker, smooth] coordinates {
    (3, 0.5)
    (4, 0.6)
    (5, 0.4)
};

\end{axis}
\end{tikzpicture}

\end{document}

verwandte Informationen