5

I'm trying to replicate the dual colours of the circles in this graph:

enter image description here

\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}

So far I have this:

enter image description here

Does anyone know of inbuilt gradient options for colouring? If not I'll settle for

Moriambar
  • 11,466
HCAI
  • 3,325

2 Answers2

7

You can declare your own plot marks using

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

For example, you could set

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


Full code:

\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}
Jake
  • 232,450
  • Many thanks again Jake! That's just what I was looking for! – HCAI Jun 20 '13 at 18:08
  • @Jake, is there an easy way of having a radial gradient and make the outer color transparent, so each point looks like a smooth point? – alfC Oct 10 '13 at 21:12
  • @alfC: Does it have to be transparent, or would white work? – Jake Oct 10 '13 at 21:13
  • Learning how to make it white will help, but the ultimate goal is to make the points smooth and able to partially supper impose in order to form complicated larger blobs without sharp borders. – alfC Oct 10 '13 at 21:15
  • @alfC: For white, you can just do \fill [ draw=white, inner color=black, outer color=white,... (or for finer control, take a look at http://tex.stackexchange.com/q/115340/2552). I'm not sure about the transparency, though, you might want to open a new question for that. – Jake Oct 10 '13 at 21:20
4

You need first to declare a shading:

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

And then a new marker:

\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}
}

So your MWE should be something like this:

\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}

And will produce this:

result

  • Many thanks for your contribution! Your approach will be useful for something else I have in mind :) – HCAI Jun 20 '13 at 18:07