5

How can I control the color of a dotplot created with pgfplots?

I tried the point meta key, but apparently I did not get it right, since it is not respected.

\begin{edit}
I would also like to have control over what colors to use.
\end{edit}

Here is what I get:

Here is what I want (via gimp):

enter image description here

And here follows the code:

\documentclass{article}


%% -------- %%
%% the data %%
%% -------- %%
\begin{filecontents}{data.tsv}
number  name    point   error_l error_r
6   foo -0.017  0.1 0.096
5   bar 0.035   0.1 0.100
4   baz 0.383   0.1 0.106
3   tmp 0.202   0.1 0.176
2   wee 0.171   0.1 0.100
1   lot 0.270   0.1 0.120
\end{filecontents}


%% -------- %%
%% packages %%
%% -------- %%
\usepackage{pgfplotstable}
\usepackage{pgfplots}





\begin{document}

%% ------------- %%
%% read the data %%
%% ------------- %%
\pgfplotstableread{data.tsv}\data


%% ------------- %%
%% plot the data %%
%% ------------- %%
\begin{tikzpicture}%
  \begin{axis}[
    y=\baselineskip,%
    scale only axis,%
    width=10cm,%
    xmin=-1,%
    xmax=1,%
    ymin=0.2,%
    ymax=6.4,%
    axis y line*=none,%
    ytick=\empty,%
    axis x line*=bottom]
    \addplot+[only marks,%
              point meta=explicit,%           <<<<< does not work
              error bars/.cd,%
              x dir=plus,%
              x explicit,%
              error mark=none,%
              error bar style={line width=1pt}]%
        table[x=point,y=number,x error=error_r,meta=number]{\data};
    \addplot+[only marks,%
              point meta=explicit,%           <<<<< does not work
              error bars/.cd,%
              x dir=minus,%
              x explicit,%
              error mark=none,%
              error bar style={line width=1pt}]%
        table[x=point,y=number,x error=error_l,meta=number]{\data};
  \end{axis}%
\end{tikzpicture}%


\end{document}

2 Answers 2

3

In order to get individually colored markers, you simply have to add the option scatter to your plots.

Unfortunately, this does not apply to error bars for which individually colored segments are unsupported (currently subject to development).

If you are satisfied if the markers have colors, this is sufficient. If you want the lines to be colored, you have the following options:

You can either wait until this is ready to use in pgfplots or you can attempt some workarounds, like a custom scatter configuration which draws the error bars together with the colored marker (compare error bar colors in scatter plot)

Concerning the choice of colors: By default, the colors are mapped into the current color map which is highly customizable. I assume that this is what you want since you provided numerical "color data". Search for 'color map' in the pgfplots manual and/or this site and you will surely find lots of hints how to adopt them.

2
  • Thanks for that answer. The coloring of the dots indeed was easy!
    – Andreas
    Jun 12, 2013 at 9:05
  • It took me some time to get the non-symmetric error bars colored with the help of the link you provided. In the end I gave up on using it in my real application, as there the error columns are created by pgfplotstable and then the solution (relying on \thisrow{}) does not work. I was also succesful in the coloring using the a colormap.
    – Andreas
    Jun 12, 2013 at 9:30
5

enter image description here

In case if you are open for the other options, here is an Asymptote solution dp.tex, which includes a datafile data.tsv and a module dotplot.asy as filecontents. The module dotplot.asy can be customized further, for example any kind of data checking, range calculations and auto-coloring feature can easily be added.

\begin{filecontents*}{data.tsv}
number  name    point   error_l error_r
6   foo -0.017  0.1 0.096
5   bar 0.035   0.1 0.100
4   baz 0.383   0.1 0.106
3   tmp 0.202   0.1 0.176
2   wee 0.171   0.1 0.100
1   lot 0.270   0.1 0.120
\end{filecontents*}
\begin{filecontents*}{dotplot.asy}
import graph;

struct DotPlot{
  int n;
  pair[] dataPoint;   
  string[] dataLabel;
  real[] leftErr;
  real[] rightErr;
  pen[] dotPen;
  real dotScale;
  frame[] mark;

  void drawDots(){
    for(int i=0;i<n;++i){ 
      draw((dataPoint[i]-(leftErr[i],0))--(dataPoint[i]+(rightErr[i],0))
        ,dotPen[i]);
      draw(dataPoint[i],marker(mark[i]));
      label("\textit{"+dataLabel[i]+"}",dataPoint[i]-(leftErr[i],0),W,dotPen[i]);
    }
  }

  void iniData(string[][] A){
    for(int i=1;i<n+1;++i){ // A[0][i] - is a title 
      dataPoint[i-1]=((real)A[2][i],(real)A[0][i]);
      dataLabel[i-1]=A[1][i];
      leftErr[i-1]=(real)A[3][i];
      rightErr[i-1]=(real)A[4][i];      
      mark[i-1]=newframe;
      filldraw(mark[i-1],scale(dotScale)*polygon(4),dotPen[i-1],dotPen[i-1]);
    }
  }

  void operator init(string fileName, pen[] dotPen,real dotScale=2){
    file fin;
    fin=input(fileName).line().word(); //  use white-space delimiters
    string[][] A=fin.dimension(0,5);
    A=transpose(A);
    this.n=A[0].length-1;  // number of data points

    this.dataPoint=new pair[n];  
    this.dataLabel=new string[n];
    this.leftErr=new real[n];
    this.rightErr=new real[n];
    this.dotPen=dotPen;    
    this.dotScale=dotScale;
    iniData(A);
  }
}
\end{filecontents*}
%
%
\documentclass[10pt,a4paper]{article}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\centering
\begin{asy}
size(200,100,IgnoreAspect);
import dotplot;
real penSize=1.2pt;
pen[] dotPen={
  red+penSize+squarecap,
  rgb(0.79,0.67,0.34)+penSize+squarecap,
  rgb(0.44,0.51,0.6)+penSize+squarecap,
  rgb(0.44,0.41,0.41)+penSize+squarecap,
  rgb(0.46,0.91,0.65)+penSize+squarecap,
  rgb(0.49,1,0)+penSize+squarecap,    
};

DotPlot dp=DotPlot("data.tsv",dotPen);
defaultpen(fontsize(10pt));
dp.drawDots();
xaxis(-0.4,1,LeftTicks(Step=0.2));
yaxis(XEquals(-0.4),0,6.5);
\end{asy}
\caption{Colored dotplot with the \texttt{Asymptote}.}
\end{figure}
\end{document} 

To process it with latexmk, create file latexmkrc:

sub asy {return system("asy '$_[0]'");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");

and run latexmk -pdf dp.tex.

1
  • Thanks a lot for that nice and complete answer! I would not mind an asymptote solution, but in this case (I did not tell) the plot is inside a table (following [tex.stackexchange.com/a/19356/15392]) and therefore I prefer a pgfplots solution which already aligns nicely with the rows.
    – Andreas
    Jun 12, 2013 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .