更改单个标记的样式 - PGFplots

更改单个标记的样式 - PGFplots

我想知道如何手动更改数据集中标记的样式。

在下面的 MWE 中,我评论了一个简单但不令人满意的解决方案(例如,用triangle代替triangle*)。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=12cm,
        height=8cm,
    ]
    \addplot+[mark=*, smooth] coordinates{
        (1,10.1)
        (2,11.7)
        (3,15.1)
        (4,17.3)
        (5,21.2)
        (6,24.5)
        (7,26.9)
        (8,28.4) % in red, scale = 2, mark=triangle*
        (9,24.0)
        (10,19.4)
        (11,13.7)
        (12,10.5)
    };
    \node [coordinate,pin=below:{red mark}]
        at (axis cs:8,28.4) {};

    %\addplot+[only marks, mark options={mark=triangle*, fill=red, color=red, xscale =2, yscale =2}] coordinates{
    %    (8,28.4)
    %};
    \end{axis}
\end{tikzpicture}
\end{document}

答案1

这里还有两个选项,均使用scatter。您可以使用@pre marker code或使用scatter/classes

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=12cm,
        height=8cm,
        scatter/@pre marker code/.code={ 
            \ifnum\coordindex=7
                \def\markopts{mark=square*,red} 
            \else
                \def\markopts{mark=*,blue}
            \fi 
            \expandafter\scope\expandafter[\markopts] },
        scatter/@post marker code/.code={\endscope},
    ]
    \addplot[scatter,draw=blue] coordinates{
        (1,10.1)
        (2,11.7)
        (3,15.1)
        (4,17.3)
        (5,21.2)
        (6,24.5)
        (7,26.9)
        (8,28.4) % in red, scale = 2, mark=triangle*
        (9,24.0)
        (10,19.4)
        (11,13.7)
        (12,10.5)
    };
    \node [coordinate,pin=below:{red mark}]
        at (axis cs:8,28.4) {};

    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        width=12cm,
        height=8cm,     
    ]
    \addplot[scatter,
        point meta=explicit symbolic, scatter/classes={
        a={mark=*,blue}, b={mark=square*,red}% <-- don't add comma
        }] coordinates{
        (1,10.1) [a]
        (2,11.7) [a]
        (3,15.1) [a]
        (4,17.3) [a]
        (5,21.2) [a]
        (6,24.5) [a]
        (7,26.9) [a]
        (8,28.4) [b] % in red, scale = 2, mark=triangle*
        (9,24.0) [a]
        (10,19.4) [a]
        (11,13.7) [a]
        (12,10.5) [a]
    };
    \node [coordinate,pin=below:{red mark}]
        at (axis cs:8,28.4) {};

    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一个直截了当的建议。


\documentclass{article}
%\usepackage{tikz} % <-- Changed (removed)
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width = 120mm, % Hint: Not needed for this example/problem.
        height = 80mm, % Hint: Not needed for this example/problem.
    ]
    % Plot 1
    \addplot[                   % <-- Changed ("+" removed)
        mark = square*,         % <-- Changed
        mark size = {5},        % <-- Changed
        mark indices = {8},     % <-- Changed
        only marks,             % <-- Changed
        % smooth,               % <-- Changed
        red,                    % <-- Changed
        ] coordinates{
        (1,10.1)
        (2,11.7)
        (3,15.1)
        (4,17.3)
        (5,21.2)
        (6,24.5)
        (7,26.9)
        (8,28.4) % Should be highlightend (index 8 of 12)
        (9,24.0)
        (10,19.4)
        (11,13.7)
        (12,10.5)
    };    
    % Plot 2
    \addplot[                                   % <-- Changed ("+" removed)
        mark = *, 
        mark indices = {1,...,7,9,10,...,12},   % <-- Changed
        smooth,
        blue,
        ] coordinates{
        (1,10.1)
        (2,11.7)
        (3,15.1)
        (4,17.3)
        (5,21.2)
        (6,24.5)
        (7,26.9)
        (8,28.4) % Should be highlightend (index 8 of 12)
        (9,24.0)
        (10,19.4)
        (11,13.7)
        (12,10.5)
    };
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容