Draw a graph in latex

(e.g.1) Simple example: y=1-x

\documentclass[tikz,border=10pt]{standalone} %border: 여백 추가 (현재 상하좌우 10pt)
\usepackage{tikz}
\usepackage{amsmath} % 수식 표현을 위한 패키지
\usetikzlibrary{patterns}
 
\begin{document}
 
\begin{tikzpicture}[scale=4]
    % Draw axes
    \draw[->] (-0.2, 0) -- (2.1, 0) node[right] {$x_1$};
    \draw[->] (0, -0.2) -- (0, 2.1) node[above] {$x_2$};
 
    \draw[thick] (1, 0.02) -- (1, -0.02) node[below left] {$1$}; % x1-axis intersection
    \draw[thick] (-0.02, 1) -- (0.02, 1) node[below left] {$1$}; % x2-axis intersection
     
    \draw[dashed] (1, -0.2) -- (1, 1.9) node[right] {};
    \draw[dashed] (-0.2, 1) -- (1.9, 1) node[right] {};
    \draw[thick, gray , domain=0:2, samples=100]
        plot(\x, {2 -\x})
        node[yshift=1.5cm]{$x_1+x_2<2$};
 
    \fill[pattern=north east lines, pattern color=gray, opacity=0.7]
        (0, 1) -- (1, 0) -- (1, 1) -- cycle;
 
    \fill[blue] (1, 1) circle (0.5pt) 
     
    \draw[thick, blue, domain=-0.2:1.3, samples=100] 
        plot (\x, {1 - \x}) ;
         
    \node[below left] at (0, 0) {$(0, 0)$};
\end{tikzpicture}
 
\end{document}

(e.g.2) More complicated example

\documentclass[tikz,border=10pt]{standalone} %border: 여백 추가 (현재 상하좌우 10pt)
\usepackage{tikz}
\usepackage{amsmath} % 수식 표현을 위한 패키지
\usetikzlibrary{patterns}
 
\begin{document}
 
\begin{tikzpicture}[scale=4]
    % Draw axes
    \draw[->] (-0.2, 0) -- (2.1, 0) node[right] {$x_1$};
    \draw[->] (0, -0.2) -- (0, 2.1) node[above] {$x_2$};
 
    \draw[thick] (1, 0.02) -- (1, -0.02) node[below left] {$1$}; % x1-axis intersection
    \draw[thick] (-0.02, 1) -- (0.02, 1) node[below left] {$1$}; % x2-axis intersection
     
    \draw[dashed] (1, -0.2) -- (1, 1.9) node[right] {};
    \draw[dashed] (-0.2, 1) -- (1.9, 1) node[right] {};
    \draw[thick, gray , domain=0:2, samples=100]
        plot(\x, {2 -\x})
        node[yshift=1.5cm]{$x_1+x_2<2$};
 
    \fill[pattern=north east lines, pattern color=gray, opacity=0.7]
        (0, 1) -- (1, 0) -- (1, 1) -- cycle;
 
    \fill[blue] (1, 1) circle (0.5pt) 
    node[above right,scale=0.8]{soft};
     
    \draw[thick, blue, domain=-0.2:1.3, samples=100] 
        plot (\x, {1 - (0^2) - \x}) ;
 
    \draw[thick, red, domain=-0.2:1, samples=100]
        plot(\x, {1 - (0^2) / (1-\x)})
        node[above, xshift=-2cm, yshift=0cm,scale=0.8] {collinear};
    \draw[thick, red, domain=-0.2:1, samples=100]
        plot(1, \x)
        node[right, xshift=0cm, yshift=-2cm,scale=0.8] {collinear};
 
    \node[below left] at (0, 0) {$(0, 0)$};
\end{tikzpicture}
 
\end{document}

(e.g.3) More more complicated example

\documentclass[tikz,border=10pt]{standalone} %border: 여백 추가 (현재 상하좌우 10pt)
\usepackage{tikz}
\usepackage{amsmath} % 수식 표현을 위한 패키지
\usetikzlibrary{patterns}
 
\begin{document}
 
\begin{tikzpicture}[scale=4]
    % Draw axes
    \draw[->] (-0.2, 0) -- (2.1, 0) node[right] {$x_1$};
    \draw[->] (0, -0.2) -- (0, 2.1) node[above] {$x_2$};
 
    \draw[thick] (0.75, 0.02) -- (0.75, -0.02) node[below] {$1 - \frac{\mu^2}{s}$}; % x1-axis intersection
    \draw[thick] (-0.02, 0.75) -- (0.02, 0.75) node[left] {$1 - \frac{\mu^2}{s}$}; % x2-axis intersection
    \draw[dashed] (1, -0.2) -- (1, 1.9) node[right] {};
    \draw[dashed] (-0.2, 1) -- (1.9, 1) node[right] {};
    \draw[thick, gray , domain=0:2, samples=100]
        plot(\x, {2 -\x})
        node[yshift=1.5cm]{$x_1+x_2<2$};
 
    \fill[pattern=north east lines, pattern color=gray, opacity=0.7]
        plot[domain=0:0.75, samples=100] (\x, {1 - (0.5^2) - \x}) --
        plot[domain=0.75:0, samples=100] (\x, {1 - (0.5^2) / (1-\x)}) -- cycle;
     
    % First equation: x_1 + x_2 = 1 - \mu^2 / s
    \draw[thick, blue, domain=-0.2:1.3, samples=100] 
        plot (\x, {1 - (0.5^2) - \x}) 
        node[below right, xshift=-0.2cm, yshift=0.8cm,scale=0.6] {$z=+1:x_1 + x_2 = 1 - \frac{\mu^2}{s}$};
 
    % Second : (1-x_1)(1-x_2) = \mu^2 / s
    \draw[thick, red, domain=-0.2:0.8, samples=100]
        plot(\x, {1 - (0.5^2) / (1-\x)})
        node[above right, xshift=-1.5cm, yshift=3.5cm,scale=0.6] {$z=-1:(1-x_1)(1-x_2) =\frac{\mu^2}{s}$};
 
    \draw[thick, red, domain=1.3:1.95, samples=100]
        plot(\x, {1 - (0.5^2) / (1-\x)});
 
    % Third : z=0.5 case
    \draw[dashed, domain=-0.2:1, samples=100]
        plot(\x, { ( \x - (1-0.25) )/ (0.25 * \x - 1) });
    \draw[dashed, domain=-0.2:1, samples=100]
        plot(\x, { ( \x - (1-0.25) )/ (0.5*(1-0.1) * \x - 1) });
    \draw[dashed, domain=-0.2:1, samples=100]
        plot(\x, { ( \x - (1-0.25) )/ (0.5*(1+0.3) * \x - 1) });
    \draw[dashed, domain=-0.2:0.9, samples=100]
        plot(\x, { ( \x - (1-0.25) )/ (0.5*(1+0.7) * \x - 1) });
     
    % Add origin
    \node[below left] at (0, 0) {$(0, 0)$};
\end{tikzpicture}
 
\end{document}

Comments

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다