5

I have drawn an axis with two dashed vertical lines:

    \begin{tikzpicture}
\begin{axis}[
    axis x line=middle, 
    axis y line=middle, 
    ymin=-6, ymax=6, ytick={1,3,5,-1,-3,-5}, ylabel=$y$, 
    xmin=-2.3, xmax=2.3, xtick={-1,-2,1,2}, xlabel=$x$
    ]
    \pgfplotsinvokeforeach{-1, 1}{
      \draw[dashed,color=red] ({rel axis cs: 0,0} -| {axis cs: #1, 0}) -- ({rel axis cs: 0,1} -| {axis cs: #1, 0});};
     \addplot[red, mark=*] coordinates {(1,1)};
     \addplot[red, mark=*] coordinates {(-1,1)};
\end{axis}
\end{tikzpicture}

I would like to shade the region between the dashed lines.

2
  • 2
    \fill ({rel axis cs: 0,0} -| {axis cs: -1, 0}) rectangle ({rel axis cs: 0,1} -| {axis cs: 1, 0});};? – Qrrbrbirlbel Sep 11 '13 at 22:44
  • @Qrrbrbirlbel Yes! If you add it as an answer, I can give you points. – Joe Johnson 126 Sep 11 '13 at 22:54
8

Here's another possibility (different from what has been suggested in comments) using \closedcycle; the axis on top=true option causes the axis to be typeset on top of the filling:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis on top=true,
    axis x line=middle, 
    axis y line=middle, 
    ymin=-6, ymax=6, ytick={1,3,5,-1,-3,-5}, ylabel=$y$, 
    xmin=-2.3, xmax=2.3, xtick={-1,-2,1,2}, xlabel=$x$
    ]
\addplot[fill=cyan,domain=-1:1,draw=none] {6} \closedcycle;
\addplot[fill=cyan,domain=-1:1,draw=none] {-6} \closedcycle;
    \pgfplotsinvokeforeach{-1, 1}{
      \draw[dashed,color=red] ({rel axis cs: 0,0} -| {axis cs: #1, 0}) -- ({rel axis cs: 0,1} -| {axis cs: #1, 0});};
     \addplot[red, mark=*] coordinates {(1,1)};
     \addplot[red, mark=*] coordinates {(-1,1)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

6

With PSTricks.

\documentclass[pstricks,border=13pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}(-2,-2)(2,2)
    \psaxes{->}(0,0)(-2,-2)(2,2)[$x$,0][$y$,90]
    \psframe*[linecolor=blue,opacity=.2](-1,-2)(1,2)
    \psset{linecolor=red,linestyle=dashed}
    \psline(-1,-2)(-1,2)
    \psline(1,-2)(1,2)
    \pscircle*(-1,1){3pt}
    \pscircle*(1,1){3pt}
\end{pspicture}
\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.