# Graphing multiple functions in the same graph

I am a beginner in latex.

I want to trace 2 functions in latex. These functions are defined by the following array of values:

X=[-1., -0.35714286 , 0.28571429 , 0.92857143 , 1.57142857 , 2.21428571 , 2.85714286 , 3.5 , 4.14285714 , 4.78571429 , 5.42857143 , 6.07142857 , 6.71428571 , 7.35714286 , 8.]

f_1(X)=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

f_2(X)=[-3.8846153846153846, -3.241758241758242, -2.598901098901099, -1.9560439560439558, -1.3131868131868127, -0.67032967032967, -0.027472527472527056, 0.6153846153846155, 1.2582417582417584, 1.901098901098902, 2.5439560439560447, 3.1868131868131884, 3.82967032967033, 4.472527472527473, 5.115384615384616]

Here is my try:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pythontex}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.75,xscale=0.6,yscale=0.6]
\draw[very thinck,->](-2,0)--(10,0) node [right];
\draw[very thinck,->](0,-20)--(0,100) node [right];
\foreach \x in {-1,0,...,8} {\draw[very thick] (\x,-0.1cm) -- (\x,0.1cm) node[below] {\x};}
\foreach \y in {-10,-8,...,10} {\draw[very thick] (-0.01cm,\y)  -- (0.01cm,\y) node[above] {\y};}
\draw[smooth,samples=3000,very thick,red] plot coordinates {(-1.0, 1), (-0.3571428571428571, 1), (0.2857142857142858, 1), (0.9285714285714288, 1), (1.5714285714285716, 1), (2.2142857142857144, 1), (2.8571428571428577, 1), (3.5, 1), (4.142857142857143, 1), (4.7857142857142865, 1), (5.428571428571429, 1), (6.071428571428572, 1), (6.714285714285715, 1), (7.357142857142858, 1), (8.0, 1)};
\draw[smooth,samples=3000,very thick,blue] plot coordinates {(-1.0, -3.8846153846153846), (-0.3571428571428571, -3.241758241758242), (0.2857142857142858, -2.598901098901099), (0.9285714285714288, -1.9560439560439558), (1.5714285714285716, -1.3131868131868127), (2.2142857142857144, -0.67032967032967), (2.8571428571428577, -0.027472527472527056), (3.5, 0.6153846153846155), (4.142857142857143, 1.2582417582417584), (4.7857142857142865, 1.901098901098902), (5.428571428571429, 2.5439560439560447), (6.071428571428572, 3.1868131868131884), (6.714285714285715, 3.82967032967033), (7.357142857142858, 4.472527472527473), (8.0, 5.115384615384616)};
\end{tikzpicture}
\end{center}
\end{document}


But the code does not compile. Can you help me please?

• Welcome to TeX.SX! Replace very thinck with very thick in your code and add at least {} to a node, so that it becomes, for example, \draw[very thick,->] (-2,0) -- (10,0) node [right] {};. Also, if you use plot coordinates, there should not be commas between the single coordinates in the list. And finally, I don't think you want \draw[very thick,->] (0,-20) -- (0,100) node [right] {}; as this will result in a veeery long vertical line. Try (0,-20) -- (0,10) maybe. Dec 12, 2023 at 15:08

Here is an alternative using the data from your original question (that I still had on disk):

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
-1,1,-3.88461538461538,10.8768115942029
-0.35714286,1,-3.24175824175824,6.5664744158533
0.28571429,1,-2.5989010989011,3.08266784974859
0.92857143,1,-1.95604395604396,0.42539189588879
1.57142857,1,-1.31318681318681,-1.40535344572612
2.21428571,1,-0.67032967032967,-2.40956817509613
2.85714286,1,-0.0274725274725271,-2.58725229222123
3.5,1,0.615384615384616,-1.93840579710145
4.14285714,1,1.25824175824176,-0.463028689736763
4.78571429,1,1.9010989010989,1.83887902987282
5.42857143,1,2.54395604395604,4.9673173617273
6.07142857,1,3.18681318681319,8.92228630582669
6.71428571,1,3.82967032967033,13.703785862171
7.35714286,1,4.47252747252747,19.3118160307601
8,1,5.11538461538462,25.7463768115942

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

\end{document}

• Sir, how to name the graph of the function? Dec 13, 2023 at 21:07
• @Karim what do you mean name? Dec 14, 2023 at 8:59

Let's get started with a MWE:

\documentclass[12pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=0.6, very thick]
\draw[->] (-1.5,0) -- (10,0) node[above] {$x$};
\draw[->] (0,-9.5) -- (0,10) node[left] {$y$};

\foreach \x in {-1,0,...,9} {
\ifnum\x=0\else
\draw (\x,-0.1cm) -- (\x,0.1cm) node[below] {\x};
\fi
}
\foreach \y in {-9,-8,...,9} {
\ifnum\y=0\else
\draw[very thick] (-0.1cm,\y)  -- (0.1cm,\y) node[left] {\y};
\fi
}

\draw[smooth, samples=100, very thick, red] plot coordinates {
(-1.0, 1)
(-0.3571428571428571, 1)
(0.2857142857142858, 1)
(0.9285714285714288, 1)
(1.5714285714285716, 1)
(2.2142857142857144, 1)
(2.8571428571428577, 1)
(3.5, 1)
(4.142857142857143, 1)
(4.7857142857142865, 1)
(5.428571428571429, 1)
(6.071428571428572, 1)
(6.714285714285715, 1)
(7.357142857142858, 1)
(8.0, 1)
};
\draw[smooth, samples=100, blue] plot coordinates {
(-1.0, -3.8846153846153846)
(-0.3571428571428571, -3.241758241758242)
(0.2857142857142858, -2.598901098901099)
(0.9285714285714288, -1.9560439560439558)
(1.5714285714285716, -1.3131868131868127)
(2.2142857142857144, -0.67032967032967)
(2.8571428571428577, -0.027472527472527056)
(3.5, 0.6153846153846155)
(4.142857142857143, 1.2582417582417584)
(4.7857142857142865, 1.901098901098902)
(5.428571428571429, 2.5439560439560447)
(6.071428571428572, 3.1868131868131884)
(6.714285714285715, 3.82967032967033)
(7.357142857142858, 4.472527472527473)
(8.0, 5.115384615384616)
};
\end{tikzpicture}
\end{document}


Some notes:

• Setting scale=0.6 is equivalent to setting xscale=0.6, yscale=0.6
• If you want to use very thick everywhere, consider setting it as option to the tikzpicture. And it is of course very thick, not very thinck.
• A node needs some contents, even if the contents are empty. So, you need to put {} after node or \node.
• \draw (0,-20) -- (0,100); will result in a very long line that is probably longer than the paper size. On the other hand, \draw (-0.01cm,\y) -- (0.01cm,\y) will result in a very short line that is hardly visible. I think, you might want to change the dimensions here.
• When using plot coordinates, there should not be commas between the single coordinates in the list. In this case here, I wonder whether it makes sense to draw a line using multiple coordinates that all have the same y value ... but I guess, you want to do something differently with this approach.
• It might not be necessary to set samples=3000. Try a smaller value first.
• How can I modify the dimensions of \y so that the step is equal for example 2 Dec 12, 2023 at 16:08
• @Karim Since the default unit in TikZ is 1cm, you can add y=2cm to the options of tikzpicture or use different values for xscale and yscale. Or do you only want to modify the plot? Then add y=2cm or yscale=2 as option to the \draw command ... But note that for more complex graphs, PGFPlots would be the better choice (see the other answer by daleif). Dec 12, 2023 at 16:15
• for your graph I would use pgfplots package
• style of graphs than can be determined by axis options,
• function coordinates I would collect in table as @daleif do in his minimalistic answer (+1),
• thick lines I would use only for functions:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
-1,             1, -3.88461538461538
-0.35714286,    1, -3.24175824175824
0.28571429,    1, -2.59890109890110
0.92857143,    1, -1.95604395604396
1.57142857,    1, -1.31318681318681
2.21428571,    1, -0.67032967032967
2.85714286,    1, -0.02747252747252
3.5,           1,  0.61538461538461
4.14285714,    1,  1.25824175824176
4.78571429,    1,  1.90109890109890
5.42857143,    1,  2.54395604395604
6.07142857,    1,  3.18681318681319
6.71428571,    1,  3.82967032967033
7.35714286,    1,  4.47252747252747
8,             1,  5.11538461538462

\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-1.5,  xmax=8.5,
ymin=-4.5,  ymax=5.5,
axis on top,
xtick distance=1,
ytick distance=1,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, samples=15,
every axis plot post/.append style={very thick}
]
\end{axis}
\end{tikzpicture}

\end{document}


• since graph present two linear function, you can draw them as straight line between first and last coordinates:
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
-1,             1, -3.88461538461538
8,             1,  5.11538461538462

\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-1.5,  xmax=8.5,
ymin=-4.5,  ymax=5.5,
axis on top,
xtick distance=1,
ytick distance=1,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, samples=2,
every axis plot post/.append style={very thick}
]