0

I tried to compile the following code to draw graphs of some functions defined by an array of values.

The code is as follows

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\pgfplotstableread[col sep=comma]{
-1,             1, -3.88461538461538,10.8768115942029,-49.37372620793093, 175.3383368168327, 
-0.35714286,    1, -3.24175824175824,6.566474415853299,-23.612493710583458, 55.10682373726086, 
 0.28571429,    1, -2.59890109890110,3.082667849748595, -6.732218933226122, -2.520938957827919, 
 0.92857143,    1, -1.95604395604396,0.4253918958887899, 2.8611214477562354, -19.7847951798583, 
 1.57142857,    1, -1.31318681318681,-1.4053534457261168,6.761550755978779, -14.825671722387444, 
 2.21428571,    1, -0.67032967032967,-2.409568175096126, 6.563092315056663, -1.6855782611049153, 
 2.85714286,    1, -0.02747252747252,-2.5872522922212347, 3.859769448605049, 9.692392646167214, 
 3.5,           1,  0.61538461538461, -1.9384057971014481, 0.24560548023911766, 13.464065559474449, 
 4.14285714,    1,  1.25824175824176, -0.4630286897367631, -2.6853762664260006, 7.884182156729922, 
 4.78571429,    1,  1.90109890109890, 1.838879029872821, -3.339152467775138, -4.693598766285728, 
 5.42857143,    1,  2.54395604395604, 4.967317361727301, -0.12169980019311577, -17.8167012959243, 
 6.07142857,    1,  3.18681318681319, 8.922286305826688, 8.561005059935194, -20.933632400670078,
 6.71428571,    1,  3.82967032967033, 13.703785862170964, 24.302985436225047,  0.606018068859953, 
 7.35714286,    1,  4.47252747252747, 19.31181603076014, 48.698264652291485, 65.55157737991703, 
 8,             1,  5.11538461538462, 25.746376811594203,83.34086603174967 ,196.75128991761915,

 
}\loadedtable

\begin{tikzpicture}
    \begin{axis}[
axis lines=center,
xmin=-1.5,  xmax=8.5,
ymin=-50,  ymax=190,
axis on top,
xtick distance=1,
ytick distance=20,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, samples=15,
every axis plot post/.append style={very thick}
                ]
\addplot table[x index=0,y index=1] {\loadedtable};
\addplot table[x index=0,y index=2] {\loadedtable};
\addplot table[x index=0,y index=3] {\loadedtable};
\addplot table[x index=0,y index=4] {\loadedtable};
\addplot table[x index=0,y index=5] {\loadedtable};
    \end{axis}
\end{tikzpicture}

\end{document}

the result:

enter image description here

I want the graphical representation to not zigzag. An idea please.

1
  • 2
    Do you have the function that created the data? One option would be to plot that instead.
    – Teepeemm
    Dec 12, 2023 at 19:40

1 Answer 1

4
  • It seems that code in your question is based on my answer to your previous question, but you not accepted it nor upvote ... It would be nice and fair, if you do this now.
  • Number of samples is to small that functions curves would appear more smooth, that it become, you need to interpolate their values between given samples.
  • This can be dan by macro smooth which by use of the spline function interpolate function between given samples.
  • Macro smooth you can add to axis option:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\pgfplotstableread[col sep=comma]{
-1,             1, -3.88461538461538,   10.8768115942029,   -49.37372620793093,     175.3383368168327,
-0.35714286,    1, -3.24175824175824,    6.566474415853299, -23.612493710583458,     55.10682373726086,
 0.28571429,    1, -2.59890109890110,    3.082667849748595,  -6.732218933226122,     -2.520938957827919,
 0.92857143,    1, -1.95604395604396,    0.4253918958887899,  2.8611214477562354,   -19.7847951798583,
 1.57142857,    1, -1.31318681318681,   -1.4053534457261168,  6.761550755978779,    -14.825671722387444,
 2.21428571,    1, -0.67032967032967,   -2.409568175096126,   6.563092315056663,     -1.6855782611049153,
 2.85714286,    1, -0.02747252747252,   -2.5872522922212347,  3.859769448605049,      9.692392646167214,
 3.5,           1,  0.61538461538461,   -1.9384057971014481,  0.24560548023911766,   13.464065559474449,
 4.14285714,    1,  1.25824175824176,   -0.4630286897367631, -2.6853762664260006,     7.884182156729922,
 4.78571429,    1,  1.90109890109890,    1.838879029872821,  -3.339152467775138,     -4.693598766285728,
 5.42857143,    1,  2.54395604395604,    4.967317361727301,  -0.12169980019311577,  -17.8167012959243,
 6.07142857,    1,  3.18681318681319,    8.922286305826688,   8.561005059935194,    -20.933632400670078,
 6.71428571,    1,  3.82967032967033,   13.703785862170964,  24.302985436225047,      0.606018068859953,
 7.35714286,    1,  4.47252747252747,   19.31181603076014,  48.698264652291485,      65.55157737991703,
 8,             1,  5.11538461538462,   25.746376811594203, 83.34086603174967,      196.75128991761915,
}\loadedtable

\begin{tikzpicture}
    \begin{axis}[
axis lines=center,
xmin=-1.5,  xmax=8.5,
ymin=-50,  ymax=190,
axis on top,
xtick distance=1,
ytick distance=20,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, samples=15, smooth,
every axis plot post/.append style={very thick}
                ]
\addplot table[x index=0,y index=1] {\loadedtable};
\addplot table[x index=0,y index=2] {\loadedtable};
\addplot table[x index=0,y index=3] {\loadedtable};
\addplot table[x index=0,y index=4] {\loadedtable};
\addplot table[x index=0,y index=5] {\loadedtable};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Edit:
However better and more accurate result you will get if for drawing would use functions (as suggested @Teepeemm in his comment), from where are calculated values collected in table. In this case you can define far more samples, for example 101 or even more and not use smooth macro anymore.

For example, that your fifth function in above MWE is defined by:

(x-3.5)^4-12*(x-3.5)^2+12

(my approximation, you should use one, from which are calculates samples collected in table) than MWE can be:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

    \begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-1.5,  xmax=8.5,
ymin=-50,  ymax=190,
axis on top,
xtick distance=1,
ytick distance=20,
tick label style={font=\footnotesize, inner sep=1pt, fill=white},
no marks, 
domain=-1:8, samples=101,                % domain and samples, no smooth macro
every axis plot post/.append style={very thick}
                ]
\addplot    {(x-3.5)^4-12*(x-3.5)^2+12}; % my approximation of your function
                                         % you should insert here your formula
\end{axis}
    \end{tikzpicture}
\end{document}

and result is:

enter image description here

Similarly you can draw other function. Just use their math expressions in \addplot instruction.

2
  • Sir, how to name the graph of the function?
    – Karim
    Dec 13, 2023 at 21:02
  • @Karim, you can named function whatever you wish. Also you can write function directly in addplot macro as I do in the second example in answer. BTW, you still not five any credits for using my code in question ...
    – Zarko
    Dec 13, 2023 at 21:25

You must log in to answer this question.

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