3

I am very new to plot in Latex, and am trying to piece together a bubble column chart with a legend and labelled axes. So far, I have a working chart, but can only get the legend or a labeled x-axis.

I think I've probably done this the hard way. I had dummy \addplot to force a legend, but that removes the axis tick labels. Is there a fix to this, or just a better way to do this?

(uncomment the two line to get the x-axis tick labels without the legend)

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}

\newif\iflegend
%\legendtrue

\usepackage{filecontents}
\begin{filecontents*}{data.dat}
1 25 1 1
1 39 0.989010989 2
2 40 0.673629243 3
2 13 0.499013807 4
4 37 0.575 5
5 98 0.994505495 2
5 108 0.64229765 3
5 6 0.583333333 5
6 1 0.96 1
6 6 0.972527473 2
6 14 0.454308094 3
7 15 0.58382643 4
7 6 0.583333333 5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}[y=.5cm]
\pgfplotstableread{data.dat}\table
\pgfplotstablegetrowsof{\table}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\table}
\pgfmathsetmacro{\N}{\pgfplotsretval-1}

\definecolor{color0}{RGB}{0,0,0}
\definecolor{color1}{RGB}{255,0,0}
\definecolor{color2}{RGB}{0,255,0}
\definecolor{color3}{RGB}{150,255,200}
\definecolor{color4}{RGB}{200,200,90}
\definecolor{color5}{RGB}{100,0,200}
\definecolor{color6}{RGB}{190,0,190}

\begin{axis}[
    scatter,
    axis lines=left,
    ylabel=Training Time,
    every axis/.append style={font=\tiny},
    x axis line style=-,
    y axis line style=-,
    xtick=data,
    xticklabels={,,Label 1,Label 2,Label 3,Label 4,Label 5},
    xticklabel style={align=left},
    width=8.6cm,
    height=9.2cm,
    xmin=0, xmax=5,
    ymin=0, ymax=110,
    legend style={
        at={(0,0)},
        anchor=south west,
        at={(axis description cs:0.15,-0.35)},
        draw=none   % no border
    },
%    legend entries={baseline,legend1,legend3,legend4,legend5,legend6},
    scatter/@pre marker code/.code={%
      \pgfplotstransformcoordinatex{\pgfplotspointmeta}%
      \scope[mark size=\pgfplotsunitxlength*\pgfmathresult]
    }
]

\iflegend
\addplot +[bar shift, area legend, color=color0] coordinates {(6,0.001)};
\addplot +[bar shift, area legend, color=color1, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color2, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color3, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color4, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color5, fill, opacity=.5, solid] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color6, fill, opacity=.5, solid] coordinates {(0,.01)};
\fi
\end{axis}

\foreach \row in {0,...,\M}{
    \foreach \col in {0,...,\N}{
        \pgfplotstablegetelem{\row}{[index]\col}\of\table
        \ifnum\col=3    % video
            \xdef\x{\pgfplotsretval}
        \fi
        \ifnum\col=1    % time
            \xdef\y{\pgfplotsretval}
        \fi
        \ifnum\col=2    % accuracy
            \xdef\radius{\pgfplotsretval}
        \fi
        \ifnum\col=0    % method
            \xdef\category{\pgfplotsretval}
        \fi
    }
    \ifnum\category=1
        \definecolor{mycolor}{RGB}{0,0,0}
    \else\ifnum\category=2
        \definecolor{mycolor}{RGB}{255,0,0}
    \else \ifnum\category=3
        \definecolor{mycolor}{RGB}{0,255,0}
    \else\ifnum\category=4
        \definecolor{mycolor}{RGB}{150,255,200}
    \else\ifnum\category=5
        \definecolor{mycolor}{RGB}{200,200,90}
    \else\ifnum\category=6
        \definecolor{mycolor}{RGB}{100,0,200}
    \else\ifnum\category=7
        \definecolor{mycolor}{RGB}{190,0,190}
    \else \definecolor{mycolor}{RGB}{128,128,128}
    \fi
    \fi
    \fi
    \fi
    \fi
    \fi
    \fi
\ifnum\category=1
\draw[mycolor] (\x*1.41,1+\y/8)circle(\radius*5mm);
\else
\fill[mycolor,opacity=.5] (\x*1.41,1+\y/8)circle(\radius*5mm);
\fi
}
\end{tikzpicture}

\end{document}

enter image description here

4

I agree that you are doing a little too hard. pgfplots already offers some alternative mechanisms that come pretty close.

\documentclass[border=4mm]{standalone}
\usepackage{filecontents} 
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}

\begin{filecontents*}{data.csv}
met time acc video
1 25 1 1
1 39 0.989010989 2
2 40 0.673629243 3
2 13 0.499013807 4
4 37 0.575 5
5 98 0.994505495 2
5 108 0.64229765 3
5 6 0.583333333 5
6 1 0.96 1
6 6 0.972527473 2
6 14 0.454308094 3
7 15 0.58382643 4
7 6 0.583333333 5
\end{filecontents*}

\definecolor{color3}{RGB}{150,255,200}
\definecolor{color4}{RGB}{200,200,90}
\definecolor{color5}{RGB}{100,0,200}
\definecolor{color6}{RGB}{190,0,190}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=left,
    ylabel=Training Time,
    every axis/.append style={font=\tiny},
    x axis line style=-,
    y axis line style=-,
    xtick=data,
    xticklabels={Label 1,Label 2,Label 3,Label 4,Label 5},
    xticklabel style={align=left},
    width=8.6cm,
    height=9.2cm,
    xmin=0, xmax=5,
    ymin=0, ymax=110,
    legend style={
        at={(0,0)},
        anchor=south west,
        at={(axis description cs:0.15,-0.35)},
        draw=none   % no border
    },
    legend entries={legend1,legend2,legend3,legend4,legend5},
    only marks,
    %legend image post style={only marks},
scatter/classes={
    1={draw=black,fill=white},
    2={fill=red},
    3={green},% etc. etc.
    4={color3},
    5={color4},
    6={color5},
    7={color6}
  },
  visualization depends on= 10*\thisrow{acc} \as\myrad,
  scatter/@pre marker code/.append style={/tikz/mark size=\myrad}
]
\addplot[scatter,scatter src=explicit,mark=*] table[x =video,y=time,meta=met] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks a lot. Can I add a scale to the circles so they appear bigger but inside the same axis footprint? – cdmh Mar 30 '15 at 15:42
  • @cmdh change the factor 10 as you wish – percusse Mar 30 '15 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.