4

I'm new to pgfplot and I have a pgfplot problem.

figure 1 :As you can see the line is going through the number '2'. How can i fix it? enter image description here

figure 2: Also can i fix the line length, going through 2, between 0 and intersection point.

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz,pgf,pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
            xlabel=$x$,
            ylabel=$y$,
            enlargelimits,
           ytick=\empty,
            xtick={2},
            %xticklabels={2},
            every axis y label/.style={at=(current axis.above origin),anchor=south},            
              every axis x label/.style={at=(current axis.right of origin),anchor=west},            
            grid = major
            ]
\addplot[name path=F,blue,domain={0:2}] {6-x^2} node[pos=0.6, above]{$f$};

\addplot[name path=G,green,domain={0:2}] {x}node[pos=.4, below]{$g$};

\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=F and G, soft clip={domain=0:2}]
;
\node[coordinate,pin=30:{$A$}] at (axis cs:3.8,3){};

\end{axis}
\end{tikzpicture}
\end{document}
  • So you actually just want a vertical line from the intersection to the x-axis? – Torbjørn T. Apr 18 '14 at 16:30
  • i need intersection to the x-axis sometimes, yes. but I also need from top of the figure to x axis from time to time. – mert Apr 18 '14 at 16:59
4

Grid lines go from the bottom of the coordinate system to the top, which is why the grid line go across the 2. There are other ways of drawing these lines though.

To get the coordinate of the intersection between the two plot lines, you can use the features of the intersections library from TikZ. This is loaded by the fillbetween library, so it is not necessary to load it specifically. Adding

name intersections={of=F and G}

to the options of a path (e.g. a \draw) will create a coordinate called intersection-1 at the (first) intersection. To draw a line from the intersection to the x-axis, you can add

\draw [name intersections={of=F and G}] ({axis cs:0,0}-|intersection-1) -- (intersection-1);

inside the axis environment. Here,

({axis cs:0,0}-|intersection-1)

means the point that has the same y-coordinate as the origin of the axis, and the same y-coordinate as intersection-1. axis cs: means that the following coordinate uses the coordinates of the axis.

To draw a line from the x-axis through the intersection to the top of the axis, you can use

\draw ({axis cs:0,0}-|intersection-1) -- ({rel axis cs:1,1}-|intersection-1);

assuming that intersection-1 has been defined. The first coordinate is as above. For the second I've used the rel axis cs coordinate system, which has (0,0) in the bottom left of the axis, and (1,1) in the top right.

Complete code and output:

\documentclass[border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
            xlabel=$x$,
            ylabel=$y$,
            enlargelimits,
            ytick=\empty,
            xtick={2},
            %xticklabels={2},
            every axis y label/.style={at=(current axis.above origin),anchor=south},            
            every axis x label/.style={at=(current axis.right of origin),anchor=west},            
            ]
\addplot[name path=F,blue,domain={0:2}] {6-x^2} node[pos=0.6, above]{$f$};

\addplot[name path=G,green,domain={0:2}] {x}node[pos=.4, below]{$g$};

\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=F and G, soft clip={domain=0:2}]
;


\draw [name intersections={of=F and G}] ({axis cs:0,0}-|intersection-1) -- (intersection-1);

\draw [thick,red,dashed] ({axis cs:0,0}-|intersection-1) -- ({rel axis cs:1,1}-|intersection-1);

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