2

My problem: The plotted line does not lie exactly on the axis...

Here is my output:

enter image description here

and here is the code to generate it:

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ axis on top=true,
         axis x line=middle,
         axis y line=middle,
         inner axis line style={=>},
                 width=6cm,height=6cm,
                     ymin=0,ymax=1.2,
                     xmin=0,xmax=1.2,
                     axis line style = thick,
                     xtick={0,1},
                     ytick={0,1},
                 every axis x label/.style={at={(current axis.right of     origin)},anchor=west},
         every axis y label/.style={at={(current axis.north west)},above=0.5mm},
                 xlabel={$p$},
                 ylabel={$q$}]
\draw[line width=4, red] (0,0)--(100,0);
\end{axis}
\end{tikzpicture}
\end{document}
  • Welcome to TeX-SX! As a new member, it is recommended to visit the Welcome and the Tour pages to be informed about our format and also to know about Minimal Example. Otherwise, more specifically to your question: could you develop a little bit more? Because, you post contains no question. Do you expect a vertical centring of the red line and the x-axis ? – R. N Jan 21 at 11:52
  • Your line does lie exact on the x-axis. The bottom half of your line is outside of the plotting area. To use axis coordinates inside (a good idea anyway) the axis, you need to set compat version - e.g. \pgfplotsset{compat=newest} – hpekristiansen Jan 21 at 12:13
4

Border of diagram clip all outsize of diagram, so you see only half of line. To solve this problem, you have two possibilities:

  • draw this line outside of diagram (see another answer)
  • use option \clip=false in axis diagram

Since this line if far longer than diagram width you also need to define its lenght accordingly. One solution, where this line is drawn width \addplot is shown on the next MWE:

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} % <---

\begin{document}

\begin{tikzpicture}
\begin{axis}[ axis on top=true,
         axis lines=middle, % <---
         %inner axis line style={=>}, % not used
                 width=6cm,height=6cm,
                     ymin=0,ymax=1.2,
                     xmin=0,xmax=1.2,
                     %axis line style = thick,
                     xtick={0,1},
                     ytick={0,1},
                 every axis x label/.style={at={(current axis.right of origin)}, anchor=west},
         every axis y label/.style={at={(current axis.north west)},above=0.5mm},
                 xlabel={$p$},
                 ylabel={$q$},                 
                 clip=false % <---
                    ]
\addplot +[line width=4pt, no marks, red] coordinates {(0,0) (1,0)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest} %added
\begin{document}
\begin{tikzpicture}
\begin{axis}[ axis on top=true,
         axis x line=middle,
         axis y line=middle,
         inner axis line style={=>},
                 width=6cm,height=6cm,
                     ymin=-0.05,ymax=1.2,  %made ymin negative, to include red line
                     xmin=0,xmax=1.2,
                     axis line style = thick,
                     xtick={0,1},
                     ytick={0,1},
                 every axis x label/.style={at={(current axis.right of     origin)},anchor=west},
         every axis y label/.style={at={(current axis.north west)},above=0.5mm},
                 xlabel={$p$},
                 ylabel={$q$}]
\draw[line width=4, red] (0,0)--(1,0); %changed form 100 to 1
\end{axis}
\end{tikzpicture}
\end{document}

Plot with red line

  • Zarko's solution with clip=false is probably better for what you want. Notice the setting of compat makes your \draw with the correct axis coordinates. – hpekristiansen Jan 21 at 12:24
2

Just move your line code before axis and outside of it:

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\draw[line width=4pt, red] (0,0)--(100,0);
\begin{axis}[ axis on top=true,
         axis x line=middle,
         axis y line=middle,
         inner axis line style={=>},
                 width=6cm,height=6cm,
                     ymin=0,ymax=1.2,
                     xmin=0,xmax=1.2,
                     axis line style = thick,
                     xtick={0,1},
                     ytick={0,1},
                 every axis x label/.style={at={(current axis.right of     origin)},anchor=west},
         every axis y label/.style={at={(current axis.north west)},above=0.5mm},
                 xlabel={$p$},
                 ylabel={$q$}]
%\draw[line width=4pt, red] (0,0)--(100,0);
\end{axis}

\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.