1

I tried to add some labels for each point in the following sample plot (that is one of the examples in Overleaf.) the labels similar to what is done in these answers.

The reason that I chose this sample code of Overleaf is that the plot in general is very similar to what I want, just the labels are missing.

I could not find on Overleaf website how to use node and pos rather than coordinates. And when I replace coordinates with node and pos like in this answer, I get an error message.

Is there a solution to minor modify this sample code so adding labels for each point?

PS. As there are different plots with varient length of axis in the paper, I prefer not to use pgfplotstableread.

\begin{tikzpicture}
\begin{axis}[
    title={Temperature dependence of CuSO\(_4\cdot\)5H\(_2\)O solubility},
    xlabel={Temperature [\textcelsius]},
    ylabel={Solubility [g per 100 g water]},
    xmin=0, xmax=100,
    ymin=0, ymax=120,
    xtick={0,20,40,60,80,100},
    ytick={0,20,40,60,80,100,120},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[ color=blue, mark=square, ] coordinates { (0,23.1)(10,27.5)(20,32)(30,37.8)(40,44.6)(60,61.8)(80,83.8)(100,114) }; \legend{CuSO(_4\cdot)5H(_2)O}

\end{axis} \end{tikzpicture}

1 Answers1

1

The option nodes near coords is probably what you are looking for:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document} \begin{tikzpicture} \begin{axis}[ title={Temperature dependence of CuSO(_4\cdot)5H(_2)O solubility}, xlabel={Temperature [\textcelsius]}, ylabel={Solubility [g per 100 g water]}, xmin=0, xmax=100, ymin=0, ymax=120, xtick={0,20,40,60,80,100}, ytick={0,20,40,60,80,100,120}, legend pos=north west, ymajorgrids=true, grid style=dashed, ]

\addplot[ color=blue, mark=square, nodes near coords, node near coord style={below right} ] coordinates { (0,23.1)(10,27.5)(20,32)(30,37.8)(40,44.6)(60,61.8)(80,83.8)(100,114) };

\legend{CuSO(_4\cdot)5H(_2)O}

\end{axis} \end{tikzpicture} \end{document}

enter image description here


According to your comments, you have more information related to each of the coordinates than just the x and y values. I would suggest that you use a table instead of a list of coordinates, because it is much easier then to add all the information as labels:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document} \begin{tikzpicture} \begin{axis}[ title={Temperature dependence of CuSO(_4\cdot)5H(_2)O solubility}, xlabel={Temperature [\textcelsius]}, ylabel={Solubility [g per 100 g water]}, xmin=0, xmax=100, ymin=0, ymax=120, xtick={0,20,40,60,80,100}, ytick={0,20,40,60,80,100,120}, legend pos=north west, ymajorgrids=true, grid style=dashed, ]

\addplot[ color=blue, mark=square, nodes near coords={$\pgfmathprintnumber\yvalue$ \ $C$: $\pgfmathprintnumber\cvalue$ \ $R$: $\pgfmathprintnumber\rvalue$ \ $S$: $\pgfmathprintnumber\svalue$}, visualization depends on={y \as \yvalue}, visualization depends on={\thisrow{c} \as \cvalue}, visualization depends on={\thisrow{r} \as \rvalue}, visualization depends on={\thisrow{s} \as \svalue}, node near coord style={font=\scriptsize, align=left}, coordinate style/.condition={ \coordindex!=1 && \coordindex!=3 }{below right}, ] table { x y c r s 0 23.1 600 200000 200 10 27.5 600 200000 200 20 32 600 200000 200 30 37.8 600 200000 200 40 44.6 600 200000 200 60 61.8 600 200000 200 80 83.8 600 200000 200 100 114 600 200000 200 };

\legend{CuSO(_4\cdot)5H(_2)O}

\end{axis} \end{tikzpicture} \end{document}

enter image description here

The same, but without the y values and with non-scientific number formatting:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document} \begin{tikzpicture} \begin{axis}[ title={Temperature dependence of CuSO(_4\cdot)5H(_2)O solubility}, xlabel={Temperature [\textcelsius]}, ylabel={Solubility [g per 100 g water]}, xmin=0, xmax=100, ymin=0, ymax=120, xtick={0,20,40,60,80,100}, ytick={0,20,40,60,80,100,120}, legend pos=north west, ymajorgrids=true, grid style=dashed, ]

\addplot[ color=blue, mark=square, nodes near coords={$C$: $\pgfmathprintnumber[fixed]\cvalue$ \ $R$: $\pgfmathprintnumber[fixed]\rvalue$ \ $S$: $\pgfmathprintnumber[fixed]\svalue$}, visualization depends on={\thisrow{c} \as \cvalue}, visualization depends on={\thisrow{r} \as \rvalue}, visualization depends on={\thisrow{s} \as \svalue}, node near coord style={font=\scriptsize, align=left}, coordinate style/.condition={ \coordindex!=1 && \coordindex!=3 }{below right}, ] table { x y c r s 0 23.1 600 200000 200 10 27.5 600 200000 200 20 32 600 200000 200 30 37.8 600 200000 200 40 44.6 600 200000 200 60 61.8 600 200000 200 80 83.8 600 200000 200 100 114 600 200000 200 };

\legend{CuSO(_4\cdot)5H(_2)O}

\end{axis} \end{tikzpicture} \end{document}

enter image description here

  • I think it could be a solution, the values of the axis Y are added automatically, but how to add a text as a customized label as well? – Questioner Feb 21 '23 at 17:26
  • @Questioner This depends on what the text should be. Maybe coordinate style/.condition={}{} could be a solution. Could you give an example what you would like the labels to look like? Do you only want to add labels to certain nodes? Another way could be point meta=explicit symbolic with meta=label, but I don't think that this works with just a coordinate list. – Jasper Habicht Feb 21 '23 at 17:28
  • A label like this one : C: 600 , R: 200000 , S: 200 for all of nodes, along =side the values that are currently are shown. – Questioner Feb 21 '23 at 17:31
  • @Questioner But how do you get these numbers for C, R and S? Where do they come from? Are you bound to using just a coordinate list or would a table also be okay? It would be much easier to store the values there ... – Jasper Habicht Feb 21 '23 at 17:36
  • These are some additional information for each point; e.g., 'C' means the number of clients etc. Y axis is the network throughput and X axis is the number of processing nodes. – Questioner Feb 21 '23 at 17:41
  • 1
    @Questioner I would rather use a table then. It will take some time to get the labels aligned in a way that everything is still readable. Maybe the suggestion in my edit can help you ... – Jasper Habicht Feb 21 '23 at 17:52
  • Is there a solution to not showing the Y axis values? (That is, showing only C, R and S) – Questioner Feb 21 '23 at 17:59
  • 1
    @Questioner Sure, you can edit to nodes near coords={$C$: $\pgfmathprintnumber\cvalue$ \\ R$: $\pgfmathprintnumber\rvalue$ \\ $S$: $\pgfmathprintnumber\svalue$}. Also, visualization depends on={y \as \yvalue} would not be needed then, of course. (See the last edit.) – Jasper Habicht Feb 21 '23 at 18:02
  • Also, if it is possible to reduce the font size of the labels, it would be be much better. Thanks. – Questioner Feb 21 '23 at 18:09
  • 1
    @Questioner Well, I leave the styling to you: You can easily change the options for node near coord style, for example using node near coord style={font=\tiny, align=left}. But I fear, the font is already quite small. – Jasper Habicht Feb 21 '23 at 18:13