1

There are some points labels that overlap on one another if the points were near an axis or close to one another, because the table is relatively long to be written horizontally.

How can I writ a point label like 1A,B2,C3,D4 above a point vertically, such that 1A comes above B2 which comes above C3, etc. So that labels do not overlap even. (Assumption: in this figure points cannot come above one another)

enter image description here

Here is how the label showed appear above a point.

enter image description here

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{%
  pointStyle/.style args={#1}{%
    color=#1,
    mark=*,
    only marks,
    mark size=4pt,
    point meta=explicit symbolic,
  }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line = bottom,
    axis y line = left,
    scaled y ticks = false,
    enlarge x limits=0.1,
    nodes near coords,
]

    \addplot[pointStyle=purple]
        coordinates {(1, 4) [\footnotesize{1A,B2,C3,D4}]};
    \addplot[pointStyle=red]
        coordinates {(2, 9) [\footnotesize{1A}]};
    \addplot[pointStyle=blue] 
        coordinates {
        (3, 6) [\footnotesize{1A,B2,C3}]
        (2.9, 6) [\footnotesize{4A,B,C3}]
        };
\end{axis}
\end{tikzpicture}
\end{document}
1

You could make the anchor a function of the \coordindex. 2nd EDIT: The simplest option in my opinion is if you do the line breaks yourself, i.e. replace the commas by \\. If you think the nodes are too tight, you could increase outer sep (for instance).

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{%
  pointStyle/.style args={#1}{%
    color=#1,
    mark=*,
    only marks,
    mark size=4pt,
    point meta=explicit symbolic,
  }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line = bottom,
    axis y line = left,
    scaled y ticks = false,
    enlarge x limits=0.1, 
    nodes near coords={\pgfplotspointmeta\vspace*{0.3\baselineskip}},
    nodes near coords style={font=\footnotesize,anchor=-90,
    align=left
    },
    %nodes near coords align={vertical},
]

    \addplot[pointStyle=purple]
        coordinates {(1, 4) [1A\\ B2\\ C3\\ D4]};
    \addplot[pointStyle=red]
        coordinates {(2, 9) [1A]};
    \addplot[pointStyle=blue, nodes near coords style={font=\footnotesize,
                anchor=-90+180*\coordindex,
            }] 
        coordinates {
        (3, 6) [1A\\ B2\\ C3]
        (2.9, 6) [4A\\ B\\ C3]
        };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Everything else is much more hacky.

1st EDIT: Sorry, I was confused by the question, hope it is closer to what you want.

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{%
  pointStyle/.style args={#1}{%
    color=#1,
    mark=*,
    only marks,
    mark size=4pt,
    point meta=explicit symbolic,
  }
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line = bottom,
    axis y line = left,
    scaled y ticks = false,
    enlarge x limits=0.1, % \begin{tabular}\end{tablue}
    nodes near coords={\vspace*{0.1\baselineskip}
    \foreach \X [count=\Y] in \pgfplotspointmeta
    {\X\newline} \vspace*{-0.7\baselineskip}
    },
    nodes near coords style={font=\footnotesize,anchor=-90,text
    width=4mm,align=center,},
    %nodes near coords align={vertical},
]

    \addplot[pointStyle=purple]
        coordinates {(1, 4) [1A,B2,C3,D4]};
    \addplot[pointStyle=red]
        coordinates {(2, 9) [1A]};
    \addplot[pointStyle=blue, nodes near coords style={font=\footnotesize,
                anchor=-90+180*\coordindex,
            }] 
        coordinates {
        (3, 6) [1A,B2,C3]
        (2.9, 6) [4A,B,C3]
        };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

In order to center the texts, you may do

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{%
  pointStyle/.style args={#1}{%
    color=#1,
    mark=*,
    only marks,
    mark size=4pt,
    point meta=explicit symbolic,
  }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line = bottom,
    axis y line = left,
    scaled y ticks = false,
    enlarge x limits=0.1, 
    nodes near coords={\vspace*{0.1\baselineskip}
    \foreach \X in \pgfplotspointmeta%
    {\centerline{\X}\newline}%
    \vspace*{-0.7\baselineskip}
    },
    nodes near coords style={font=\footnotesize,anchor=-90,
    text width=1cm
    },
    %nodes near coords align={vertical},
]

    \addplot[pointStyle=purple]
        coordinates {(1, 4) [1A,B2,C3,D4]};
    \addplot[pointStyle=red]
        coordinates {(2, 9) [1A]};
    \addplot[pointStyle=blue, nodes near coords style={font=\footnotesize,
                anchor=-90+180*\coordindex,
            }] 
        coordinates {
        (3, 6) [1A,B2,C3]
        (2.9, 6) [4A,B~~,C3]
        };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

If you want everything centered, remove the ~~ after B. It is amazing how much hackery is required here, basically because one cannot easily have a \\ inside a \foreach loop. There are ways to avoid this, e.g. here but they are fairly complicated and, in what I tried, destroyed the coloring, and so does trivlist. Crazy...

  • Well, but in my case, I want the labels to be written vertically above the point like the second picture I have attached. Your solution is little bit confusing because I don't know which label belongs to which point in the two blue points. An other thing the purple point label overlap the x-axis. – Taha Magdy Sep 4 '18 at 1:04
  • 1
    @TahaMagdy Better now? – user121799 Sep 4 '18 at 1:37
  • @marmort, there is a small issue though, the vertical label lines are aligned to the left; how can I make every line centered. Here is a image – Taha Magdy Sep 4 '18 at 2:06
  • 1
    @TahaMagdy I added a few options. The top one would be my personal favorite. You can finetune parameters like outer seo in order to shift the text around. – user121799 Sep 4 '18 at 2:47
  • My question is completely solved now. Thank You @marmot – Taha Magdy Sep 4 '18 at 2:57

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.