1

After reading a question on "How to draw the region of inequality?", I have a code like this that refers to the inequalities

inequality

\documentclass{article}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{MnSymbol}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{patterns}

\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={
             \pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque,
                          grid=none}\pgfplotsdrawaxis}}}

\newcommand{\drawge}{-- (rel axis cs:1,0) -- (rel axis cs:1,1) -- (rel axis cs:0,1) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:1,1) -- (rel axis cs:1,0) -- (rel axis cs:0,0) \closedcycle}

\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[axis line style=thin,
axis x line=center,
axis y line=center,
ymin=-7, ymax=7, xmin=-7, xmax=7, xlabel=\(x\), ylabel=\(y\), grid=minor]
\addplot[name path global=line1, pattern=north west lines, color=blue!50, thick, domain=-7:7]{3*x-5};
\addplot[name path global=line2, pattern=north west lines, color=blue!50, thick, domain=-7:7]{6-2*x};
\addplot [draw=none, pattern=northeast lines, pattern color=blue!40, domain=-7:7]{3*x-5} \drawle;
\addplot [draw=none, pattern=northeast lines, pattern color=blue!40, domain=-7:7]{6-2*x} \drawge;
\end{axis}
\end{tikzpicture}
\end{figure}


\end{document}

which looks like

picture

I want it to look like

picture2, where the graph is filled only where all the inequalities hold true. NOTE the two graphs are entirely different problems. I only want the style, not the question.

However, the code for the latter image uses intersections:

\begin{figure}[ht]
\centering
\begin{tikzpicture}
    \begin{axis}[axis line style=thin,
    axis x line=center,
    axis y line=center,
    ymin=-6, ymax=7, xmin=-6, xmax=11, xlabel=\(x\), ylabel=\(y\), grid=minor]
    \addplot[name path global=line1, color=blue!50, thick, domain=-5:10]{5/3-x/3};
    \addplot[name path global=line2, color=blue!50, thick, domain=-5:10]{x/5-10/7};
    \addplot[name path global=line3, color=blue!50, thick, domain=-5:10]{6*x+10};
    \fill[name intersections={of=line1 and line2,by=point1}, name intersections={of=line1 and line3,by=point2}, name intersections={of=line2 and line3,by=point3}][very thick, pattern=northeast lines, draw=blue!60, pattern color=blue!30!cyan](point1)--(point2)--(point3)--(point1);
    \end{axis}
\end{tikzpicture}
\end{figure}

The first image has only one intersecting point, hence I cannot use this method. I also don't want other areas shaded- only the final areas where the lines intersect. Is there a way around it without to find the points in which the lines intersect the border of the grid, meaning I have to manually compute the points?

EDIT: added the inequalities for the graph.

  • 1
    I am not sure what the final result should look like. Could you edit the first plot manually and mark the stuff that should be shaded and which ones not? – Stefan Pinnow Feb 7 '17 at 11:29
  • Please see my edit, I put the inequalities. – Matthew Tang Feb 8 '17 at 3:15
  • your two inequalities can not determine your desired (second) image. Please, draw your equation by hand to see this. – Zarko Feb 8 '17 at 11:25
  • @Zarko, I only want the first graph to have overlaps filled in (the other areas to be blank). I do not want the code for the second graph (I have the code). I just want the style of the code for the first graph. – Matthew Tang Feb 8 '17 at 11:52
  • @MatthewTang, then both answer should be satisfactory ... anyway, I will add some more elements to my answer soon. – Zarko Feb 8 '17 at 11:55
3

Edit: Since the graph are relatively simple, it is sensible to manually calculate coordinates of border lines at limits ymin=-7 and ymax=7 and their intersection:

  • for line 3*x-5 are (-2/3,-7), (4,7)
  • for line -2x+6 are (1/2,7), (6.5,-7)
  • intersection both lines is at (11/5,8/5).

With known this point drawing desired image become simple. First draw path, which limit pattern area and than draw both lines:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{patterns}

\begin{document}
\begin{figure}[ht]
    \centering
\begin{tikzpicture}[E/.style={font=\small,text=black, sloped, pos=0.84}]
    \begin{axis}[
axis line style=thin, axis lines=center,
xmin=-5, xmax=7,
ymin=-7, ymax=7,  
ytick={-6,-4,...,6},
xlabel=$x$, ylabel=$y$,
grid=major,
                    ]
\path[pattern=north west lines, pattern color=blue!40]
(4,7) -- (11/5,8/5) -- (6.5,-7) -- (6.5,7) -- cycle;
\draw[very thick, color=blue!50]
    (-2/3,-7) -- (4.0, 7) node[E,above] {$y\leq  3x-5$}
    (-0.5, 7) -- (6.5,-7) node[E,below] {$y\geq -2x+6$};
    \end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

enter image description here

Of course, you can calculate intersection of both lines by use of intersections library, as Suggested JPi in his answer or consider the following slightly different pure pgfplots solution. The differences to the first example and JPi answer are designated by % <-- :

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14, set layers}% <-- also works with 1.11
\usetikzlibrary{intersections, patterns}

\begin{document}
\begin{figure}[ht]
    \centering
\begin{tikzpicture}[E/.style={font=\small,text=black, sloped, pos=0.75}]
    \begin{axis}[
axis line style=thick, % <--
     axis lines=center,% <--
    axis on top,
xmin=-5, xmax=7,
ymin=-7, ymax=7,
ytick={-6,-4,...,6},
xlabel=$x$, ylabel=$y$,
grid=major,% <--
every axis plot post/.append style={very thick, color=blue!50}% <--
                    ]
\addplot[name path=A,domain=-2:5] { 3*x-5} node[E,above] {$y\leq  3x-5$};
\addplot[name path=B,domain=-2:7] {-2*x+6} node[E,below] {$y\geq -2x+6$};
\path[name path=C] (0,7) -| (7,-7) -- (0,-7); 
    \path[name intersections={of=A and B, by={i1}}];
    \path[name intersections={of=A and C, by={i2}}];% <--
    \path[name intersections={of=B and C, by={i3}}];% <--
\pgfonlayer{pre main}% <--
\path[pattern=north west lines, pattern color=red]% <-- red is for better seeing that patterns are in background
    (i2) -- (i1) -- (i3) |- (i2);
\endpgfonlayer
\end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

which gives almost the same result as it is obtained in the first example:

enter image description here

  • Sorry, I meant the overlapping. Please see my edit, I put the inequalities. – Matthew Tang Feb 8 '17 at 3:15
  • Hi, one problem I encountered. I could not use the compat=1.14 mode for pgfplotsset. The compiler at sharelatex doesn't recognise it. I get the graph but no pattern fill. – Matthew Tang Feb 9 '17 at 2:07
  • I'm not familiar with sharelatex, so don't know which version of pgfplots is there installed. About this please contact sharelatex people. I test code with pgfplots 1.11 and MWE works. Try replace 1.14 with newest, maybe than will work. – Zarko Feb 9 '17 at 10:23
3

You mean something like this?

enter image description here

\documentclass{article}


\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{patterns}

\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={
             \pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque,
                          grid=none}\pgfplotsdrawaxis}}}

\newcommand{\drawge}{-- (rel axis cs:1,0) -- (rel axis cs:1,1) -- (rel axis cs:0,1) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:1,1) -- (rel axis cs:1,0) -- (rel axis cs:0,0)\closedcycle}

\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[axis line style=thin,
axis x line=center,
axis y line=center,
ymin=-7, ymax=7, xmin=-7, xmax=7, xlabel=\(x\), ylabel=\(y\), grid=minor]
\addplot[name path global=line1, pattern=north west lines, color=blue!50, thick, domain=-7:7]{3*x-5};
\addplot[name path global=line2, pattern=north west lines, color=blue!50, thick, domain=-7:7]{6-2*x};
\addplot [draw=none, pattern=north east lines, pattern color=blue!40, domain=-7:7,samples=200]{min(3*x-5,6-2*x)} \drawle;
\addplot [draw=none, pattern=north east lines, pattern color=blue!40, domain=-7:7, samples=1000]{max(6-2*x,3*x-5)}\drawge;
\end{axis}
\end{tikzpicture}
\end{figure}


\end{document}
  • Sorry, I meant the overlapping. Please see my edit, I put the inequalities. – Matthew Tang Feb 8 '17 at 3:15

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.