1

I have a graph I'm trying to display and need to label each coordinate with the given values. For a couple of the labels, the text overlaps the line. For just a single line, I could probably move the label position. However, I typically have multiple lines per graph, so that's not always feasible.

Using my MWE, is there a way to make the background for the node labels white so that the text and the lines don't overlap? Note that I am using the methodology given in this previous answer.

\documentclass[10pt]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{capt-of}    

\begin{document}

\begin{center}
    \begin{tikzpicture}
    \begin{axis}[legend pos=south east,
        xlabel style={align=center},
        xlabel={Horizontal Subgrade Modulus, (lb/in)/in},
        ymin=600,
        ylabel={Maximum Moment, lb-in (N-m)},
        height=0.3\textheight, 
        width=0.9\columnwidth,
        cycle list name=mark list*,
        legend columns=2
        ]
            \addplot [nodes near coords,mark=*, %S1
                visualization depends on=\thisrow{alignment} \as \alignment,
                every node near coord/.style={anchor=\alignment},
                point meta=explicit symbolic,]
                table [meta index=2]{
                    x   y   label   alignment
                    94  637 {\footnotesize 637 (71.94)} -110
                    185 766 {\footnotesize 766 (86.59)} -90
                    357 885 {\footnotesize 885 (100.02)}    90
                };

        \legend {S1}
    \end{axis}
    \end{tikzpicture}
    \captionof{figure}{MAXIMUM MOMENT --- 4 IN. DIAMETER PIPE}
    \label{4_Mom}
\end{center}


\end{document}

graph

  • 1
    It looks like adding \tikzset{every node/.style={fill=white}} right before \begin{tikzpicture} adds the white background, but the padding and positioning leave a lot to be desired. Maybe something along those lines, but nicer looking? – grfrazee Mar 27 '17 at 18:09
1

You can add it to the nodes that you are placing, through every node near coord or to every node (for all nodes). Also, you can play with the inner sep and outer sep to reduce the white space around your labels.

Also, from the previous question, you can play with the padding and xshift and yshift for better positioning.

\documentclass[preview]{standalone}

\usepackage{pgfplots}
\usepackage{caption}

\begin{document}

\begin{center}
    \begin{tikzpicture}
    \begin{axis}[legend pos=south east,
        xlabel style={align=center},
        xlabel={Horizontal Subgrade Modulus, (lb/in)/in},
        ymin=600,
        ylabel={Maximum Moment, lb-in (N-m)},
        height=0.3\textheight, 
        width=0.9\columnwidth,
        cycle list name=mark list*,
        legend columns=2
        ]
            \addplot [nodes near coords,mark=*, %S1
                visualization depends on=\thisrow{alignment} \as \alignment,
                every node near coord/.style={anchor=\alignment, fill=white},
                point meta=explicit symbolic,]
                table [meta index=2]{
                    x   y   label   alignment
                    94  637 {\footnotesize 637 (71.94)} -110
                    185 766 {\footnotesize 766 (86.59)} -90
                    357 885 {\footnotesize 885 (100.02)}    90
                };

        \legend {S1}
    \end{axis}
    \end{tikzpicture}
    \captionof{figure}{MAXIMUM MOMENT --- 4 IN. DIAMETER PIPE}
    \label{4_Mom}
\end{center}


\end{document}

Remember to provide packages and document class next time for your MWE.

  • Thanks, I noticed the missing packages just a minute ago and added them back in. Copied/pasted too many times and missed it the last time. – grfrazee Mar 27 '17 at 18:18
  • @grfrazee, I do not understand you logic. so I delete my answer. – Zarko Mar 27 '17 at 20:28
2

If you really have "a lot" of other lines with nodes near coords in your plot and only want to avoid that the text cannot be written properly, you could also use the contour package to draw the nodes near coords, instead of using a white background for nodes as suggested in adn's answer. Play with the \contourlength so it fits your needs best.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage[outline]{contour}
    % define the length of the contour lines
    \contourlength{0.2em}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        legend pos=south east,
        xlabel style={align=center},
        xlabel={Horizontal Subgrade Modulus, (lb/in)/in},
        ymin=600,
        ylabel={Maximum Moment, lb-in (N-m)},
        cycle list name=mark list*,
        nodes near coords={
            % add the contour command to the `nodes near coords' output
            % (change the color to see what really is happening)
            \contour{white}{\pgfplotspointmeta}
        },
        visualization depends on=\thisrow{alignment} \as \alignment,
        node near coords style={
            font=\footnotesize,
            anchor=\alignment,
        },
        point meta=explicit symbolic,
    ]
        \addplot table [meta index=2] {
            x   y   label          alignment
            94  637 {637 (71.94)}  -110
            185 766 {766 (86.59)}  -90
            357 885 {885 (100.02)} 90
        };

        \legend {S1}
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Ooh, I like the effect this produces. Having it contour the individual glyphs instead of a white block in the background is pretty neat. I'll have to keep this in mind. – grfrazee Mar 29 '17 at 15:31

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.