I would like to plot a 2D $x_1 - x_2$ coordinate system and a shaded polygon specified by

$ −3x_1 + 4x_2 \leq 4, 3x_1 + 2x_2 \leq 11, 2x_1 − x_2 \leq 5, x_1 , x_2 \geq 0 $

How to draw this using TikZ?

Thanks and regards!


I just learned something from this post (or if you have better idea, please don't hesitate to reply).

Here is my plot and I was wondering how to add different shades to the polygon enclosed by the solid lines and to the polygon enclosed by the dashed lines? alt text

Here is my code:


    \draw[very thin,color=gray] (-0.1,-0.1) grid (4.9,3.1);    
    \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x_1$};
    \draw[->] (0,-0.2) -- (0,3.2) node[above] {$x_2$};

    \draw[domain=0:3] plot (\x,{1+ 0.75 * \x}) node[above right] {$-3x_1+4x_2 =4$};
    \draw[domain=1.5:4] plot (\x,{5.5 - 1.5 * \x}) node[below right] {$3x_1 + 2x_2 = 11$};
    \draw[domain=2:4] plot (\x,{-5+2 * \x}) node[below right] {$2x_1 - x_2 =5$}; 

    \node at (2,3) {(2, 2.5)};
    \node at (3.5,1) {(3, 1)};
    \node at (3,-0.25) {(2.5, 0)};    
    \node at (-0.25,-0.25) {(0, 0)};
    \node at (-0.5,1) {(0, 1)};

    \draw[dashed] (0,1) -- (2,2);
    \draw[dashed] (2,2) -- (3,1);
    \draw[dashed] (3,1) -- (2,0); 
    \draw[dashed] (2,0) -- (0,0); 
    \draw[dashed] (0,0) -- (0,1); 

    \node at (2,1.5) {(2, 2)};
    \node at (2,-0.25) {(2, 0)};

  • Does \filldraw help? Commented Dec 2, 2010 at 1:11
  • @Matthew: Thanks \filldraw helps! Two questions: (1) when using \filldraw, do I always have to provide the coordinates of each vertices? (2) is it possible to give the picture drawn by tikzpicture environment numbering in the document, caption and label just as we can do for table environment? Thanks!
    – Tim
    Commented Dec 2, 2010 at 3:25
  • I don't quite understand (1). \fill just needs some closed path that in can fill. How you create that is up to you. For (2), simply put it into a figure environment, as you would with and other graphic.
    – Caramdir
    Commented Dec 2, 2010 at 3:55

3 Answers 3


Here's another way to do it without using intersections but using path clipping. Each path we draw, we also define a clip against this path. This does mean "drawing" the path twice: once to draw and once to clip against (and I don't see a quick way of merging these two since they have to happen at different times). One thing I like about this approach is that what the graphical package is doing is precisely what the mathematics is doing. That is, each inequality specifies a "clipping" of the plane, saying "After this, we're only interested in one side of this line and we throw away everything else.". That's exactly what a clip does.

clipped polygon



\tikzset{axis/.style={ultra thick, Red!75!black, -latex, shorten <=-\nudge cm, shorten >=-2*\nudge cm}}

\draw[axis] (0,0) -- (4,0) node[right=2* \nudge cm] {\(x_1\)};
\draw[axis] (0,0) -- (0,4) node[above=2*\nudge cm] {\(x_2\)};
\clip (-\nudge ,-\nudge) rectangle (4+\nudge,4+\nudge);
\draw[line] (0,1) -- (4,4) coordinate (ineq1);
\draw[line] (0,5.5) -- (4,-.5) coordinate (ineq2);
\draw[line] (0,-5) -- (4,3) coordinate (ineq3);
\clip (0,1) -- (4,4) |- (0,0);
\clip (0,5.5) -- (4,-.5) -| (0,0);
\clip (0,-5) -- (4,3) |- (4,4) -| (0,0);
\fill[Red,opacity=.5] (0,0) rectangle (4,4);
\draw[dashed,line] (0,1) -- (2,2) -- (3,1) -- (2,0);
\clip (0,1) -- (2,2) -- (3,1) -- (2,0) -| (0,1);
\fill[Blue,opacity=.5] (0,0) rectangle (4,4);
\node[above right] at (ineq1) {\(\mathllap{-}3 x_1 + 4 x_2 = 4\)};
\node[below right] at (ineq2) {\(3 x_1 + 2 x_2 = 11\)};
\node[above right] at (ineq3) {\(2 x_1 - x_2 = 5\)};
\foreach \coord/\adj in {
  {(0,0)}/below left,
  {(2,0)}/below left,
  {(2.5,0)}/below right,
} {
  \fill \coord circle (2pt) node[\adj] {\coord};

Other aspects of this answer:

  1. We use clipping to avoid having to work out the formulae for the lines too carefully (and we draw these as simple lines rather than using the plot function).
  2. We use node-coordinates when we want to label the lines since putting the labels directly on the lines wouldn't work with the clipping.
  3. Note the use of \mathllap to adjust the placement of one of the labels (that's why mathtools is included).
  4. Note the use of a negative shortening of the axes.
  • @ Andrew: Where and how does one go to start to learn how to use the tikz drawing tool package? I am interested in being able to create graphics my own without any software like maple or java. Thanks
    – night owl
    Commented Jul 24, 2011 at 20:04
  • @night owl: If there's anything specific, you could ask a question here. But for general learning, start with the resources listed in the answers to the question: tex.stackexchange.com/q/15779/86 (and some of the linked questions to that). Commented Jul 25, 2011 at 10:06

Here is one way to do it. The code uses intersections to calculate the vertices of the polygon (note that the intersection library is only available starting from TikZ v2.10). The labels are generated using the let operation, which gives access to coordinates of nodes (thank you Antal). Filling paths is described in section 15.4 “Filling a Path” of the TikZ v2.10 manual. You could also apply various patterns.


% print a point given by two coordinates in pt (output is in cm)
    \pgfmathparse{0.03514598035*#1}\pgfmathprintnumber{\pgfmathresult}, %

    % grid and axes
    \draw[very thin,color=gray] (-0.1,-0.1) grid (4.1,3.1);    
    \draw[->,name path=xaxis] (-0.2,0) -- (4.2,0) node[right] {$x_1$};
    \draw[->,name path=yaxis] (0,-0.2) -- (0,3.2) node[above] {$x_2$};

    % lines  
    \draw[name path=line1,domain=0:3] plot (\x,{1+ 0.75 * \x}) node[above right] {$-3x_1+4x_2 =4$};
    \draw[name path=line2,domain=1.5:4] plot (\x,{5.5 - 1.5 * \x}) node[below right] {$3x_1 + 2x_2 = 11$};
    \draw[name path=line3,domain=2:4] plot (\x,{-5+2 * \x}) node[below right] {$2x_1 - x_2 =5$}; 

    % calculate intersection points
    \node[name intersections={of=line1 and line2}] (a) at (intersection-1) {};
    \node[name intersections={of=line2 and line3}] (b) at (intersection-1) {};
    \node[name intersections={of=line3 and xaxis}] (c) at (intersection-1) {};
    \node (d) at (0,0) {};
    \node[name intersections={of=yaxis and line1}] (e) at (intersection-1) {};

    % draw the big polygon    
    \filldraw[ultra thick,fill=green!80!black,fill opacity=0.4] (a.center) -- (b.center) -- (c.center) -- (d.center)  -- (e.center) -- cycle;

    % label the vertices
    \path let \p0 = (a) in node [left=0.1cm of a] {\printpoint{\x0}{\y0}};
    \path let \p0 = (b) in node [right=0cm of b] {\printpoint{\x0}{\y0}};
    \path let \p0 = (c) in node [below right=0cm and -0.1cm of c.center] {\printpoint{\x0}{\y0}};
    \path let \p0 = (d) in node [below left=0cm of d.center] {\printpoint{\x0}{\y0}};
    \path let \p0 = (e) in node [left=0cm of e.center] {\printpoint{\x0}{\y0}};

    % draw the small polygon   
    \filldraw[thick,dashed,fill=blue,fill opacity=0.4] (0,1) -- (2,2) -- (3,1) -- (2,0) -- (0,0) -- cycle; 


  • @Caramdir: Thanks, really nice to know about the intersection library! Questions: what does " (a) at (intersection-1)" mean? Isn't "intersection-1" representing the intersection? What is the difference between "intersection-1" and "a" then?
    – Tim
    Commented Dec 2, 2010 at 4:04
  • Since (intersection-1) is only valid during one command, we need to create a node at the intersection point for later reference. I simply chose a as the name of that node. One can usually read tikz commands as sentences. This line translates to “Create a node, using the intersection of line1 and line2, with name a at the intersection point and do not put any content in it”.
    – Caramdir
    Commented Dec 2, 2010 at 4:39
  • Thanks again! I actually have not been able to run your latex code yet, because of errors reported by latex. There might be some package or library missing. I report the error in this new post tex.stackexchange.com/questions/6381/…. Really appreciate if you or someone can have a look. Thank!
    – Tim
    Commented Dec 2, 2010 at 5:00
  • @Tim Sorry, I forgot that the intersection library is only available in TikZ v2.10 (which is currently the newest version).
    – Caramdir
    Commented Dec 2, 2010 at 5:22
  • @Caramdir: I think you should be able to do the following to use the computed labels: \draw let \p0 = (a) in node [left=0.1cm of a] {$(\p0)$};. However, I can't compile your code for some reason (plot's not working—probably an old version of TikZ, though it's odd that intersection works and plot doesn't), so I can't test it. Commented Dec 2, 2010 at 8:21

My solution uses my new package tkz-fct and tkz-euclide but it's not necessary to use tkz-fct because it's possible with straight lines to get points without gnuplot :( . If you want directly the lines you defin a point with tkzDefpoint(x,y){Name} and the you can draw a line with tkzDrawLine(A,B) . Here my code with tkz-fct

remark : for a line samples =2 is enough and it's possible !


         \tkzInit \tkzClip[space=.5]
       \tkzDrawX[label=$x_1$] \tkzDrawY[label=$x_2$]
       \tkzFct[color   = red,domain =0:10,samples=2]{4+0.75*x}
       \tkzDefPointByFct(0) \tkzGetPoint{A1} 
       \tkzDefPointByFct(10) \tkzGetPoint{B1} 
       \tkzFct[color   = blue,domain =0:10,samples=2]{11-1.5*x}
       \tkzDefPointByFct(0) \tkzGetPoint{A2} 
       \tkzDefPointByFct(10) \tkzGetPoint{B2}  
       \tkzFct[color   = green,domain =0:10,samples=2]{2*x-5}
       \tkzDefPointByFct(2.5) \tkzGetPoint{A3} 
       \tkzDefPointByFct(10) \tkzGetPoint{B3} 
       \tkzInterLL(A1,B1)(A2,B2) \tkzGetPoint{I12} 
       \tkzInterLL(A1,B1)(A3,B3) \tkzGetPoint{I13}
       \tkzInterLL(A3,B3)(A2,B2) \tkzGetPoint{I23}

Polygon Alain Matthes

You must log in to answer this question.

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