3

Is it possible to apply a shading effect on one side of a polyline in TikZ? I have the following simple code

\begin{tikzpicture}
\draw (1.06,9.56) -- (1.44,9.78) -- (1.78,9.56) -- (2.14,9.79) -- (2.48,9.29)
          -- (2.83,9.79) -- (3.17,9.54) -- (3.54,9.76) -- (3.87,9.56);

\draw (1.06,8.94) -- (1.44,8.72) -- (1.78,8.93) -- (2.14,8.71) -- (2.48,9.21) 
          -- (2.83,8.72) -- (3.17,8.96) -- (3.54,8.74) -- (3.87,8.94);
\end{tikzpicture}

I want some shading above the upper line and below the lower line, as the lines represent air-solid boundaries and I need to convey this graphically.

2 Answers 2

7

I believe that a fill or shade requires a closed path up to some \MaxY and down to some \MinY so you can just create one. Below I used a macro to define the \TopPath and \BottomPath so that we can use it twice: Once to draw the line, then to specify the closed area.

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\begin{document}

\newcommand*{\MaxX}{3.87}%
\newcommand*{\MinX}{1.06}%
\newcommand*{\MaxY}{10.0}%
\newcommand*{\MinY}{8.50}%
\newcommand*{\TopPath}{(\MinX,9.56) -- (1.44,9.78) -- (1.78,9.56) -- (2.14,9.79) -- (2.48,9.29) -- (2.83,9.79) -- (3.17,9.54) -- (3.54,9.76) -- (\MaxX,9.56)}%
\newcommand*{\BottomPath}{(\MinX,8.94) -- (1.44,8.72) -- (1.78,8.93) -- (2.14,8.71) -- (2.48,9.21) -- (2.83,8.72) -- (3.17,8.96) -- (3.54,8.74) -- (\MaxX,8.94)}%

\begin{tikzpicture}
\shade [top color= white, bottom color=brown] \TopPath
          -- (\MaxX,\MaxY) -- (\MinX,\MaxY) -- cycle;
\draw [thick] \TopPath;


\shade [top color= blue, bottom color=white] \BottomPath
          -- (\MaxX,\MinY) -- (\MinX,\MinY) -- cycle;
\draw [thick] \BottomPath;
\end{tikzpicture}
\end{document}
2
  • I see what you did here, that's what I need!
    – MindV0rtex
    Commented Feb 15, 2012 at 23:44
  • @MindV0rtex: Have updated it so that you can reuse the coordinates without having to specify them twice. Commented Feb 16, 2012 at 0:17
7

I know that an answer has already been accepted, but I will still give mine.

This answer uses a clip, to remove parts of the boundaries that are drawn (the outside parts). With this approach a path only has to be entered and used once.

Here is the code

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

\begin{scope} %only to make sure the clip doesn't act on anything else
\clip ($(0,0)+2*(\pgflinewidth,\pgflinewidth)$) rectangle ($(5,3)-2*(\pgflinewidth,\pgflinewidth)$);

\draw[ultra thick,bottom color=gray,top color = white] (0,2.5) -- (1,2.7) -- (2,2.4) -- (3,2.6) -- (4,2.5) -- (5,2.6) -- (5,3) -- (0,3) -- cycle;

\draw[ultra thick,bottom color=white,top color = gray] (0,0.5) -- (1,0.6) -- (2,0.4) -- (3,0.6) -- (4,0.5) -- (5,0.4) -- (5,0) -- (0,0) -- cycle;
\end{scope}

\end{tikzpicture}

\end{document}

The result is

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .