
我正在嘗試自訂散點圖標記的形狀+顏色。當我對顏色(例如紅色)進行硬編碼時,Everythink 就可以工作:
\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}
然而,我正在努力為每個圖單獨製作顏色。我可以定義
\newcommand{\myStyle}[1]{%
% define marker shape here
\tikzset{marker/.style={circle, inner sep=1.5pt, fill=#1}}
}
但我想我必須指定區塊內的顏色
\pgfdeclareplotmark{mymarker}{%
\node[marker] {};
}
變更每個 addplot 指令的顏色。但是,我不確定如何將顏色作為參數傳遞給命令 \pgfdeclareplotmark。
答案1
編輯1
\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}
像這樣 ?
\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}