6
\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    view={110}{45},
    hide x axis, 
    hide z axis, 
    hide y axis,    
    ]

\addplot3[only marks,scatter,mark=cube*,mark size=20] coordinates {%
    (0,0,0) (0,0,1) (0,0,2) (1,0,0) (1,0,1) (1,0,2) (2,0,0) (2,0,1) (2,0,2) 
    (0,1,0) (0,1,1) (0,1,2) (1,1,0) (1,1,1) (1,1,2) (2,1,0) (2,1,1) (2,1,2) 
    (0,2,0) (0,2,1) (0,2,2) (1,2,0) (1,2,1) (1,2,2) (2,2,0) (2,2,1) (2,2,2) 
    } ;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Edit 1

Empirical try with xmin, xmax ... and view, but if some on knows a more scientific approach ... Anyway seems too long in z direction ?

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=3.5,ymin=0,ymax=7.5,zmin=0,zmax=5.5,
    view={113}{20},
    hide x axis, 
    hide z axis, 
    hide y axis,    
    ]


\addplot3[only marks,scatter,mark=cube*,mark size=20] coordinates {%
    (0,0,0) (0,0,1) (0,0,2) (1,0,0) (1,0,1) (1,0,2) (2,0,0) (2,0,1) (2,0,2) 
    (0,1,0) (0,1,1) (0,1,2) (1,1,0) (1,1,1) (1,1,2) (2,1,0) (2,1,1) (2,1,2) 
    (0,2,0) (0,2,1) (0,2,2) (1,2,0) (1,2,1) (1,2,2) (2,2,0) (2,2,1) (2,2,2) 
    } ;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\newcommand{\MyCube}[1][]{%
\begin{scope}[very thin,line cap=round,line join=round,#1]  
\draw[fill=blue!20] (0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle ;
\draw[fill=red!20] (1,0,0)--(1,0,1)--(1,1,1)--(1,1,0)--cycle ;
\draw[fill=yellow!20] (0,1,0)--(0,1,1)--(1,1,1)--(1,1,0)--cycle ;
\end{scope}
}

\newcommand{\LineCubeX}[3][]{%
    \begin{scope}[shift={#2}]
    \foreach \x in {0,1,...,\the\numexpr#3-1} {%
    \MyCube[shift={(\x,0,0)},#1] }
    \end{scope}
    }
\newcommand{\LineCubeY}[3][]{%
    \begin{scope}[shift={#2}]
    \foreach \y in {0,1,...,\the\numexpr#3-1} {%
    \MyCube[shift={(0,\y,0)},#1] }
    \end{scope}
    }
\newcommand{\LineCubeZ}[3][]{%
    \begin{scope}[shift={#2}]
    \foreach \z in {\the\numexpr#3-1,\the\numexpr#3-2,...,0} {%
    \MyCube[shift={(0,0,\z)},#1] }
    \end{scope}
    }

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}[z=.4cm]

\foreach \z in {3,2,1,0} {%
    \foreach \y in {0,1,2,3} {%
        \foreach \x in {0,1,2,3} {%
            \MyCube[shift={(\x,\y,\z)},opacity=.7] }}}

\end{tikzpicture}

\begin{tikzpicture}[z=.4cm]
% Face arrière  
\LineCubeX{(0,0,8)}{9} ;
\LineCubeY{(0,1,8)}{7} ;
\LineCubeY{(8,1,8)}{7} ;
\LineCubeX{(0,8,8)}{9} ;
% montants horizontaux
\LineCubeZ{(0,0,1)}{7} ;
\LineCubeZ{(8,0,1)}{7} ;
\LineCubeZ{(8,8,1)}{7} ;
\LineCubeZ{(0,8,1)}{7} ;
% face avant
\LineCubeX{(0,0,0)}{9} ;
\LineCubeY{(0,1,0)}{7} ;
\LineCubeY{(8,1,0)}{7} ;
\LineCubeX{(0,8,0)}{9} ;
\end{tikzpicture}

\end{document}

enter image description here

Rico
  • 6,097
Tarass
  • 16,912
  • 1
    I think, marks of a scatter plot are not the most effective way to perform that task. Check out this answer http://tex.stackexchange.com/questions/12020/what-is-the-easiest-way-to-draw-3d-cube-with-tikz for ways to plot cubes in tikz. – papabravo Feb 24 '14 at 09:42
  • It is a first step. I plan to make cubes with holes in them, and plot small cubes is a easy way to do that. I don't need to draw on huge cube but a puzzle of cubes. – Tarass Feb 24 '14 at 09:52
  • To keep the same view angle, an empirical solution of \begin{axis}[xmin=0,xmax=4.2,ymin=0,ymax=7.1,zmin=0,zmax=3.5, view={110}{45},... works. No doubt, those max numbers are related to the trigonometric functions of the 110,45 view angle. – Steven B. Segletes Feb 24 '14 at 11:38
  • Thank you but I give up with pgfplots for this, I will draw my cubes directly. I add a new edit. – Tarass Feb 24 '14 at 12:40

1 Answers1

3

There is probably a better way to do this, but I used the referenced code to build a custom plot mark.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    view={110}{45},
    hide x axis, 
    hide z axis, 
    hide y axis,    
    ]

\pgfdeclareplotmark{realcube}{
\pgfmathsetmacro{\cubex}{100}
\pgfmathsetmacro{\cubey}{100}
\pgfmathsetmacro{\cubez}{100}
\draw[fill] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[fill] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[fill] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
    }

\addplot3[only marks,scatter,mark=realcube,] coordinates {%
    (0,0,0) (0,0,1) (0,0,2) (1,0,0) (1,0,1) (1,0,2) (2,0,0) (2,0,1) (2,0,2) 
    (0,1,0) (0,1,1) (0,1,2) (1,1,0) (1,1,1) (1,1,2) (2,1,0) (2,1,1) (2,1,2) 
    (0,2,0) (0,2,1) (0,2,2) (1,2,0) (1,2,1) (1,2,2) (2,2,0) (2,2,1) (2,2,2) 
    } ;
\end{axis}
\end{tikzpicture}

\end{document}

I don't know yet, why the cube dimensions must be 100 and not 1. This only works, if your scatter grid has a grid width 1.

Result

papabravo
  • 1,192
  • Thank's, but it doesn't look like a cube. ;-) I thought about scales factors in x,y or z directions, but it doesn't work. – Tarass Feb 24 '14 at 10:14