4

I need to create a scatter chart similar to that in this question but using text labels instead of numerics. i.e. from that example, the nodes data column should contain text label (with spaces) and these need to be on the chart.

Then, I'd like to remove the colored marker, and change the color of the text based on label column in the data file.

Is this possible, please?

enter image description here

[Edit] Here's the code

\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.7}
\begin{filecontents*}{mydata.dat}
nodes     x         y       label
1.0000    14.1209   7.0332  a
2.0000     0.6367   16.6166 a
3.0000     5.5385   11.7053 a
4.0000     0.9234   10.9945 a
5.0000     1.9426   18.3439 b
6.0000    16.4692    5.7168 b
7.0000    13.8966   15.1440 a
8.0000     6.3420   15.0746 a
9.0000    19.0044    7.6089 b
10.0000    0.6889   11.3564 b
\end{filecontents*}

\begin{document}

 \begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}[xlabel=metri,ylabel=metri]
        \addplot[
                visualization depends on={\thisrow{nodes}\as\myvalue},
            scatter/classes={
                a={mark=*,blue},
                b={mark=*,red}
                },
                scatter, only marks,
                scatter src=explicit symbolic,
                nodes near coords*={\pgfmathprintnumber[int detect]\myvalue},]
         table[x=x,y=y,meta=label]
            {mydata.dat};
    \end{axis}
\end{tikzpicture}

\end{figure}
\end{document}
  • Yes it is possible. Can you paste thecode into the question so that we can work on it by editing your question? – percusse Jun 1 '15 at 14:14
5

You can use the same machinery with the marks being the text you wish to plot and then you can control the color etc. with your scatter classes. For nonnumerical items you need to add value keyword to the visualization depends on key to turn off the math parsing.

\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.12}
\begin{filecontents*}{data.txt}
nodes     x         y       label
{spaced label}   14.1209   7.0332  a
{spaced label}    0.6367   16.6166 a
{spaced label}    5.5385   11.7053 a
{spaced label}    0.9234   10.9945 a
{spaced label}    1.9426   18.3439 b
{spaced label}   16.4692    5.7168 b
{spaced label}   13.8966   15.1440 a
{spaced label}    6.3420   15.0746 a
{spaced label}   19.0044    7.6089 b
{spaced label}    0.6889   11.3564 b
\end{filecontents*}

\begin{document}

 \begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}[xlabel=metri,ylabel=metri]
        \addplot+[
                visualization depends on={value \thisrow{nodes}\as\myvalue},
                scatter/classes={
                a={mark=text,text mark=\myvalue,blue},
                b={mark=text,text mark=\myvalue,red}
                },
                scatter,draw=none,
                scatter src=explicit symbolic]
         table[x=x,y=y,meta=label]
            {data.txt};
    \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

  • @cdmh My pleasure. – percusse Jun 2 '15 at 0:33

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.