Search code examples
latexpgfplots

How to plot a scatterplot and label points using a text column in Tikz Pgf


I want to plot a scatter plot and label points using text picked from the table which I paste from notepad.

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread[]{
x   y   label  
23  9.6 Jan
46  11.3 Feb
60  12.8 Mar
54  9.8 Apr
}\advdata

\begin{figure}[H]
\begin{center}
\caption{Scatterplot of Advertising versus Sales Data}
\vspace*{15mm}
\scalebox{1}{
\begin{tikzpicture}[]
\begin{axis}[
axis lines = left,
xlabel = {Advertising (x)}, ylabel = {Sales (y)}
]
\addplot[
mark=*,only marks,
point meta =explicit symbolic,
nodes near coords* = {\label},
]
table[x=x,y=y, meta =label]{\advdata};
\end{axis}
\end{tikzpicture}
}
\end{center}
\end{figure}
\end{document}

I get this error: ! Package PGF Math Error: Could not parse input 'Jan' as a floating point numbe r, sorry. The unreadable part was near 'Jan'..

How can I make this thing work?


Solution

  • Using just nodes near coords:

    \documentclass[12pt]{article}
    \usepackage{tikz}
    \usepackage{pgfplots}
    \pgfplotsset{compat=newest} 
    \usepackage{pgfplotstable}
    \usepackage{float}
    
    \begin{document}
    
    \pgfplotstableread[]{
    x   y   label
    23  9.6 Jan
    46  11.3 Feb
    60  12.8 Mar
    54  9.8 Apr
    }\advdata
    
    \begin{figure}[H]
    \centering
    \caption{Scatterplot of Advertising versus Sales Data}
    \begin{tikzpicture}[]
    \begin{axis}[
    axis lines = left,
    xlabel = {Advertising (x)}, ylabel = {Sales (y)}
    ]
    \addplot[
    mark=*,only marks,
    point meta =explicit symbolic,
    nodes near coords,
    ]
    table[x=x,y=y, meta =label]{\advdata};
    \end{axis}
    \end{tikzpicture}
    \end{figure}
    \end{document}
    

    enter image description here