tikz pgfplot 标记中的渐变颜色

tikz pgfplot 标记中的渐变颜色

我正在尝试复制此图中圆圈的双色:

在此处输入图片描述

\documentclass[tikz,border={40pt 12pt}]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}[trim axis left,trim axis right]

\begin{axis}[%
width=4.52083333333333in,
height=1.9359375in,
scale only axis,
xmin=0,
xmax=2,
xlabel={Partition},
ymin=0,
ymax=5,
ytick={1,...,5},
xtick={0,0.5,...,2},
ylabel={No Partition}
]
\addplot [
color=red,
only marks,
mark=ball,
mark options={solid, line width=1pt},
forget plot
]
table[row sep=crcr]{
0.752891515 1.119390244\\
0.918362178 1.65402439\\
0.794259181 4.527682927\\
1.10865344 0.515142276\\
1.621612495 1.002439024\\
};
\addplot [
color=black,
dashed,
line width=1pt,
forget plot
]
table[row sep=crcr]{
0.6 2.64921945346413\\
0.7 2.44758628403132\\
0.8 2.24595311459851\\
0.9 2.04431994516571\\
1 1.8426867757329\\
1.1 1.64105360630009\\
1.2 1.43942043686729\\
1.3 1.23778726743448\\
1.4 1.03615409800167\\
1.5 0.834520928568865\\
1.6 0.632887759136058\\
};
\end{axis}
\end{tikzpicture}%
\end{document}

到目前为止我有这个:

在此处输入图片描述

有人知道内置渐变着色选项吗?如果没有,我会选择

答案1

你可以使用以下方式声明自己的情节标记

\pgfdeclareplotmark{<name>}{<code that draws the marks>}

例如,你可以设置

\pgfdeclareplotmark{gradient}{
    \fill [
        draw=none,
        left color=red,
        right color=blue,
        shading angle=45
    ] (0,0) circle [radius=4pt];
}


完整代码:

\documentclass[tikz, border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}

\pgfdeclareplotmark{gradient}{
    \fill [
        draw=none,
        left color=red,
        right color=blue,
        shading angle=45
    ] (0,0) circle [radius=\pgfplotmarksize];
}

\begin{axis}[%
width=4.52083333333333in,
height=1.9359375in,
scale only axis,
xmin=0,
xmax=2,
xlabel={Partition},
ymin=0,
ymax=5,
ytick={1,...,5},
xtick={0,0.5,...,2},
ylabel={No Partition}
]
\addplot [
only marks, mark=gradient, mark size=5pt
]
table[row sep=crcr]{
0.752891515 1.119390244\\
0.918362178 1.65402439\\
0.794259181 4.527682927\\
1.10865344 0.515142276\\
1.621612495 1.002439024\\
};
\addplot [
color=black,
dashed,
line width=1pt,
forget plot
]
table[row sep=crcr]{
0.6 2.64921945346413\\
0.7 2.44758628403132\\
0.8 2.24595311459851\\
0.9 2.04431994516571\\
1 1.8426867757329\\
1.1 1.64105360630009\\
1.2 1.43942043686729\\
1.3 1.23778726743448\\
1.4 1.03615409800167\\
1.5 0.834520928568865\\
1.6 0.632887759136058\\
};
\end{axis}
\end{tikzpicture}%
\end{document}

答案2

您首先需要声明一个阴影:

\pgfdeclarehorizontalshading{myshading}{2cm}{color(0pt)=(blue); color(2cm)=(yellow)} 

然后是一个新标记:

\pgfdeclareplotmark{mymarker}{%
    \begin{pgfscope}
        \pgfpathcircle{\pgfpointorigin}{\pgfplotmarksize}
        \pgfusepath{clip}
        \pgfpathrectangle{\pgfpoint{-\pgfplotmarksize}{-\pgfplotmarksize}}{\pgfpoint{2\pgfplotmarksize}{2\pgfplotmarksize}}
        \pgfshadepath{myshading}{-45}
        \pgfusepath{stroke}
    \end{pgfscope}
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcircle{\pgfpointorigin}{\pgfplotmarksize}
    \pgfusepath{stroke}
}

因此你的 MWE 应该是这样的:

\documentclass[tikz,border={40pt 12pt}]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8} 

\pgfdeclarehorizontalshading{myshading}{2cm} 
    {color(0pt)=(blue); color(2cm)=(yellow)} 

\pgfdeclareplotmark{mymarker}{%
    \begin{pgfscope}
        \pgfpathcircle{\pgfpointorigin}{\pgfplotmarksize}
        \pgfusepath{clip}
        \pgfpathrectangle{\pgfpoint{-\pgfplotmarksize}{-\pgfplotmarksize}}{\pgfpoint{2\pgfplotmarksize}{2\pgfplotmarksize}}
        \pgfshadepath{myshading}{-45}
        \pgfusepath{stroke}
    \end{pgfscope}
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcircle{\pgfpointorigin}{\pgfplotmarksize}
    \pgfusepath{stroke}
}


\begin{document}
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[%
width=4.52083333333333in,
height=1.9359375in,
scale only axis,
xmin=0,
xmax=2,
xlabel={Partition},
ymin=0,
ymax=5,
ytick={1,...,5},
xtick={0,0.5,...,2},
ylabel={No Partition}
]


\addplot [
color=red,
only marks,
mark=mymarker,
forget plot
]
table[row sep=crcr]{
0.752891515 1.119390244\\
0.918362178 1.65402439\\
0.794259181 4.527682927\\
1.10865344 0.515142276\\
1.621612495 1.002439024\\
};
\addplot [
color=black,
dashed,
line width=1pt,
forget plot
]
table[row sep=crcr]{
0.6 2.64921945346413\\
0.7 2.44758628403132\\
0.8 2.24595311459851\\
0.9 2.04431994516571\\
1 1.8426867757329\\
1.1 1.64105360630009\\
1.2 1.43942043686729\\
1.3 1.23778726743448\\
1.4 1.03615409800167\\
1.5 0.834520928568865\\
1.6 0.632887759136058\\
};
\end{axis}
\end{tikzpicture}%
\end{document}

并将产生以下结果:

结果

相关内容