2

This is a scatter plot, where x-axis is categorical and y-axis is numerical. I want to draw a perpendicular line from the point (X, 9) to (X, 0). I have tried \draw[doshed] (X, 0) -- (X, 9); but it didn't work.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}


\begin{document}

\begin{tikzpicture}
    \begin{semilogyaxis}[
            symbolic x coords={X},
            xtick=data,
            xticklabel style = {font=\footnotesize},
            enlargelimits=0.2,
            nodes near coords,
            every node near coord/.append style={xshift=0.5cm, yshift=0.1cm},
            y tick label style={/pgf/number format/.cd,%
            set thousands separator={,}, fixed},
          ]

      \addplot[color=blue,mark=*, only marks,
        point meta=explicit symbolic] coordinates {
        (X,     9) [9\%]
    };

        % THE LINE I WANT TO DRAW
        % \draw[doshed] (X, 0) -- (X, 9); 

    \end{semilogyaxis}%
\end{tikzpicture}%



\end{document}
1

You can use the ycomb handler to achieve what you want. For details, please have a look at the comments in the code. (Please also note that I have removed all the unnecessary parts of your code.)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{semilogyaxis}[
        symbolic x coords={X,Y},
        xtick=data,
        nodes near coords,
    ]

        \addplot [
            mark=*,
            only marks,
            point meta=explicit symbolic,
            % -------------------------------------------------------------
            % use `ycomb' to draw a vertical line at each data point
            ycomb,
            % state the color and line style of the vertical line
            draw=black,
            dashed,
            % because the above commands also influence the `mark' style,
            % we need to reset it to what we want
            every mark/.append style={
                solid,
                blue,
            },
            % -------------------------------------------------------------
        ] coordinates {
            (X, 9)  [9\%]
            (Y, 10) [bla]
        };

    \end{semilogyaxis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • This is more elegant. However the Ulrike Fischer's solution does work. – Taha Magdy Aug 17 '18 at 2:13
3
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}


\begin{document}

\begin{tikzpicture}
    \begin{semilogyaxis}[
            symbolic x coords={X},
            xtick=data,
            xticklabel style = {font=\footnotesize},
            enlargelimits=0.2,
            nodes near coords,
            every node near coord/.append style={xshift=0.5cm, yshift=0.1cm},
            y tick label style={/pgf/number format/.cd,%
            set thousands separator={,}, fixed},
          ]

      \addplot[color=blue,mark=*, only marks,
        point meta=explicit symbolic] coordinates {
        (X,     9) [9\%]
    };
        \node[circle,fill,inner sep=1.6pt, red] at (axis cs:X,12) {};

        % THE LINE I WANT TO DRAW
         \draw[dashed] (axis cs:X, 0) -- (axis cs:X, 9);

    \end{semilogyaxis}%
\end{tikzpicture}%



\end{document}

enter image description here

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.