1

I was trying to understand how the programming tools work on Latex in the tikzpicture environment. Can someone explain me how to reduce this big code to a cleaner version that can be write with a simpler and less "manual" code?

Thanks for helping! E

\documentclass[]{standalone}
%for pgf images
\usepackage{amsmath}
\usepackage{tikz} % To generate the plot from csv
\usetikzlibrary{positioning}
\usepackage{pgfplots}
%
\definecolor{mycolor1}{rgb}{0.23921,0.86274,0.59215}%
%
\begin{document}
%
\begin{tikzpicture}

\begin{axis}[%
width=5cm,
height=5cm,
scale only axis,
plot box ratio=1 1 1,
point meta min=1,
point meta max=2,
%unbounded coords=jump,
xmin=0.5,
xmax=1.5,
xtick={\empty},
tick align=outside,
y dir=reverse,
ymin=0.5,
ymax=15,
ytick={ 0, 5, 10, 15},
zmin=0,
zmax=56.5486677646163,
ztick={18.8495559215388,37.6991118430775,56.5486677646163},
zticklabels={{},{},{},{},{},{}},
view={-130.835728529777}{25.8},
%axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left,
axis z line*=left,
xmajorgrids,
ymajorgrids,
zmajorgrids
]

\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 0.5 0\\
    0.5 1.5 0\\
    1.5 1.5 0\\
    1.5 0.5 0\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 1.5 0\\
    0.5 1.5 0.698131700797732\\
    1.5 1.5 0.698131700797732\\
    1.5 1.5 0\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 1.5 0.698131700797732\\
    0.5 2.5 0.698131700797732\\
    1.5 2.5 0.698131700797732\\
    1.5 1.5 0.698131700797732\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 2.5 0.698131700797732\\
    0.5 2.5 1.39626340159546 \\
    1.5 2.5 1.39626340159546 \\
    1.5 2.5 0.698131700797732\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 2.5 1.39626340159546\\
    0.5 3.5 1.39626340159546\\
    1.5 3.5 1.39626340159546\\
    1.5 2.5 1.39626340159546\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 3.5 1.39626340159546\\
    0.5 3.5 2.0943951023932\\
    1.5 3.5 2.0943951023932\\
    1.5 3.5 1.39626340159546\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 3.5 2.0943951023932\\
    0.5 4.5 2.0943951023932\\
    1.5 4.5 2.0943951023932\\
    1.5 3.5 2.0943951023932\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 4.5 2.0943951023932\\
    0.5 4.5 2.79252680319093\\
    1.5 4.5 2.79252680319093\\
    1.5 4.5 2.0943951023932\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 4.5 2.79252680319093\\
    0.5 5.5 2.79252680319093\\
    1.5 5.5 2.79252680319093\\
    1.5 4.5 2.79252680319093\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 5.5 2.79252680319093\\
    0.5 5.5 3.49065850398866\\
    1.5 5.5 3.49065850398866\\
    1.5 5.5 2.79252680319093\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 5.5 3.49065850398866\\
    0.5 6.5 3.49065850398866\\
    1.5 6.5 3.49065850398866\\
    1.5 5.5 3.49065850398866\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 6.5 3.49065850398866\\
    0.5 6.5 4.18879020478639\\
    1.5 6.5 4.18879020478639\\
    1.5 6.5 3.49065850398866\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 6.5 4.18879020478639\\
    0.5 7.5 4.18879020478639\\
    1.5 7.5 4.18879020478639\\
    1.5 6.5 4.18879020478639\\
};
\addplot3[%
patch,patch type=rectangle,table/row sep=\\,patch table={0 1 2 3\\}, fill=mycolor1, faceted color=black, z buffer=sort] 
table[row sep=crcr] {%
    x   y   z\\
    0.5 7.5 4.18879020478639\\
    0.5 7.5 4.88692190558412\\
    1.5 7.5 4.88692190558412\\
    1.5 7.5 4.18879020478639\\
};
\end{axis}
%\begin{axis}[%
%width=17.896in,
%height=13.688in,
%at={(0in,0in)},
%scale only axis,
%xmin=0,
%xmax=1,
%ymin=0,
%ymax=1,
%axis line style={draw=none},
%ticks=none,
%axis x line*=bottom,
%axis y line*=left
%]
%\end{axis}
\end{tikzpicture}%
\end{document}%

enter image description here

1
  • 2
    Welcome. // Without having run your code, i.e. assuming no compile errors, that's how it looks like in pgfplots. // If you relate to a dofferent problem, a screenshot of the result and some sketch may be useful. Commented May 20, 2025 at 13:31

1 Answer 1

1

Here is a short version that looks very much like what you need:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\definecolor{mycolor1}{rgb}{0.23921,0.86274,0.59215}

\begin{document}
\begin{tikzpicture}
\def\step{0.698131700797732}

\begin{axis}[
    width=5cm, height=5cm,
    scale only axis, plot box ratio=1 1 1,
    xmin=0.5, xmax=1.5,
    ymin=0.5, ymax=15,
    zmin=0, zmax=56.5486677646163,
    y dir=reverse,
    view={-130.835728529777}{25.8},
    axis x line*=bottom, axis y line*=left, axis z line*=left,
    xmajorgrids, ymajorgrids, zmajorgrids
]

\foreach \k in {0,...,6} {
    \pgfmathsetmacro{\z}{\k*\step}
    \pgfmathsetmacro{\yone}{0.5 + \k}
    \pgfmathsetmacro{\ytwo}{\yone + 1}
    \addplot3[fill=mycolor1, draw=black] coordinates {
        (0.5, \yone, \z)
        (1.5, \yone, \z)
        (1.5, \ytwo, \z)
        (0.5, \ytwo, \z)
        (0.5, \yone, \z) % Замыкаем контур
    };
}

\end{axis}
\end{tikzpicture}
\end{document} 

enter image description here

1
  • That is great! Thanks for the explanation, I could complete the figure I had in mind with very few steps! Commented May 21, 2025 at 9:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.