3D 笛卡尔坐标系

3D 笛卡尔坐标系

我想修改我的三维笛卡尔坐标系,如附图所示。

  1. 我希望我的勾号下有数字。
  2. 我希望网格的各部分与 x_1 轴平行。

我的代码需要做哪些更改?

在此处输入图片描述

梅威瑟:

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{center}
\begin{tikzpicture}
\draw[gray!60!white, thick] (-4.99,-4.99) grid (9.99,9.99);
\draw[->, >=latex, ultra thick] (0,0,-2.6) -- (0,0,13) node[left]{$x_1$};
\draw[->, >=latex, ultra thick] (0,0,0) -- (10,0,0) node[below]{$x_2$};
\draw[->, >=latex, ultra thick] (0,0,0) -- (0,10,0) node[left]{$x_3$};
\foreach \z in {2.6,5.2,7.8,10.4}
\draw[shift={(0,0,\z)}, ultra thick] (0pt,0pt,0pt) -- (0pt,-0.21pt,0pt);
\foreach \x in {2,4,6,8}
\draw[shift={(\x,0)}, ultra thick] (0pt,0pt) -- (0pt,-6pt);
\foreach \y in {2,4,6,8}
\draw[shift={(0,\y)}, ultra thick] (0pt,0pt) -- (-6pt,0pt);
\draw[fill=green!50!white, nearly transparent] (0,0,0) -- (0,0,12.7) -- (0,9.9,12.7) -- (0,9.9,0);
\draw[fill=blue!50!white, nearly transparent] (0,0,0) -- (9.9,0,0) -- (9.9,9.9,0) -- (0,9.9,0);
\draw[fill=red!50!white, nearly transparent] (0,0,0) -- (9.9,0,0) -- (9.9,0,12.7) -- (0,0,12.7);
\draw[blue, ultra thick, ->] (4,0,5.2) -- (4,4,5.2);
\draw[red, ultra thick, ->] (0,0,5.2) -- (4,0,5.2);
\draw[green, ultra thick, ->] (0,0,0) -- (0,0,5.2);
\shade[ball color=yellow] (4,4,5.2) circle (0.2);
\end{tikzpicture}
\end{center}

\end{document}

答案1

具有一些样式和declare function常量。网格通过一个循环手动绘制,带有标签的刻度通过第二个循环绘制。使用参数scale调整图表的大小。

在此处输入图片描述

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}

\begin{center}
\begin{tikzpicture}[
  scale=2,
  axisstyle/.style={
    ->,
    >=latex,
    ultra thick
  },
  gridstyle/.style={
    gray!60!white, thick
  },
  tickstyle/.style={
    ultra thick
  },
  axisbg/.style={
    fill=#1!50,
    nearly transparent
  },
  arrows/.style={
    ultra thick,
    ->,
    #1
  },
  declare function={
    ticklen=0.15;
    xmax=5;
    ymax=5;
    zmax=5;
  },
]    

\draw[axisstyle] (0,0,-1) -- (0,0,zmax) node[left]{$x_1$};
\draw[axisstyle] (0,0,0) -- (xmax,0,0) node[below]{$x_2$};
\draw[axisstyle] (0,0,0) -- (0,ymax,0) node[left]{$x_3$};

\foreach \x in {0.5,1,...,4.5}
{
  \draw [gridstyle] (0,0,\x) -- (xmax,0,\x);
  \draw [gridstyle] (0,0,\x) -- (0,ymax,\x);

  \draw [gridstyle] (\x,0,0) -- (\x,0,zmax);
  \draw [gridstyle] (\x,0,0) -- (\x,ymax,0);

  \draw [gridstyle] (0,\x,0) -- (0,\x,zmax);
  \draw [gridstyle] (0,\x,0) -- (xmax,\x,0);
}
\foreach \x in {1,...,4}
{
  \draw [tickstyle] (0,0,\x) -- (0,-ticklen,\x) node[below] {$\x$};

  \draw [tickstyle] (\x,0,0) -- (\x,-ticklen,0) node[below] {$\x$};

  \draw [tickstyle] (0,\x,0) -- (-ticklen,\x,0) node[left] {$\x$};
}

\fill[axisbg=green] (0,0,0) -- (0,0,zmax) -- (0,ymax,zmax) -- (0,ymax,0);
\fill[axisbg=blue]  (0,0,0) -- (xmax,0,0) -- (xmax,ymax,0) -- (0,ymax,0);
\fill[axisbg=red]   (0,0,0) -- (xmax,0,0) -- (xmax,0,zmax) -- (0,0,zmax);

\draw[arrows=blue]  (2,0,2) -- (2,2,2);
\draw[arrows=red]   (0,0,2) -- (2,0,2);
\draw[arrows=green] (0,0,0) -- (0,0,2);

\shade[ball color=yellow] (2,2,2) circle[radius=0.1];

\end{tikzpicture}
\end{center}

\end{document}

答案2

快速解决方案:

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{center}
\begin{tikzpicture}
%\draw[gray!60!white, thick] (-4.99,-4.99) grid (9.99,9.99);
\draw[->, >=latex, ultra thick] (0,0,-2.6) -- (0,0,13) node[left]{$x_1$};
\draw[->, >=latex, ultra thick] (0,0,0) -- (10,0,0) node[below]{$x_2$};
\draw[->, >=latex, ultra thick] (0,0,0) -- (0,10,0) node[left]{$x_3$};
\foreach \z/\zc in {2.6/1,5.2/2,7.8/3,10.4/4}{
\draw[shift={(0,0,\z)}, ultra thick] (0pt,0pt,0pt) -- (0pt,-0.21pt,0pt)node[below]{\zc};
\draw[gray!60!white, thick](0,0,\z)--++(0:10);
\draw[gray!60!white, thick](0,0,\z)--++(90:10);}
\foreach \x/\xc in {2/1,4/2,6/3,8/4}{
\draw[shift={(\x,0)}, ultra thick] (0pt,0pt) -- (0pt,-6pt)node[below]{\xc};
\draw[gray!60!white, thick](\x,0)--++(0:10);
\draw[gray!60!white, thick](\x,0)--++(90:10);
\draw[gray!60!white, thick](\x,0)--++(-135:7);}
\foreach \y/\yc in {2/1,4/2,6/3,8/4}{
\draw[shift={(0,\y)}, ultra thick] (0pt,0pt) -- (-6pt,0pt)node[left]{\yc};
\draw[gray!60!white, thick](0,\y)--++(0:10);
\draw[gray!60!white, thick](0,\y)--++(-135:7);}
\draw[fill=green!50!white, nearly transparent] (0,0,0) -- (0,0,12.7) -- (0,9.9,12.7) -- (0,9.9,0);
\draw[fill=blue!50!white, nearly transparent] (0,0,0) -- (9.9,0,0) -- (9.9,9.9,0) -- (0,9.9,0);
\draw[fill=red!50!white, nearly transparent] (0,0,0) -- (9.9,0,0) -- (9.9,0,12.7) -- (0,0,12.7);
\draw[blue, ultra thick, ->] (4,0,5.2) -- (4,4,5.2);
\draw[red, ultra thick, ->] (0,0,5.2) -- (4,0,5.2);
\draw[green, ultra thick, ->] (0,0,0) -- (0,0,5.2);
\shade[ball color=yellow] (4,4,5.2) circle (0.2);
\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

答案3

既然您正在加载 pgfplots,为什么不使用它呢?它使用一个简单的选项来处理所有网格。

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[x={(10,0)},y={(0,10)},z={({-10/sqrt(8)},{-10/sqrt(8)})},
grid = both,minor tick num=4,xmin=0,xmax=9.9,ymin=0,ymax=9.9,zmin=0,zmax=12.7,
ticks=none,clip=false,tick style={opacity=0}]
\draw[->, >=latex, ultra thick] (0,0,-2.6) -- (0,0,13) node[left]{$x_1$};
\draw[->, >=latex, ultra thick] (0,0,0) -- (10,0,0) node[below]{$x_2$};
\draw[->, >=latex, ultra thick] (0,0,0) -- (0,10,0) node[left]{$x_3$};
\draw[fill=green!50!white, nearly transparent] (0,0,0) -- (0,0,12.7) -- (0,9.9,12.7) -- (0,9.9,0);
\draw[fill=blue!50!white, nearly transparent] (0,0,0) -- (9.9,0,0) -- (9.9,9.9,0) -- (0,9.9,0);
\draw[fill=red!50!white, nearly transparent] (0,0,0) -- (9.9,0,0) -- (9.9,0,12.7) -- (0,0,12.7);

\addplot3[->,no marks,blue,ultra thick] coordinates {(4,0,5.2) (4,4,5.2)};
\addplot3[->,no marks,red,ultra thick] coordinates {(0,0,5.2) (4,0,5.2)};
\addplot3[->,no marks,green,ultra thick] coordinates {(0,0,0) (0,0,5.2)};
\shade[ball color=yellow] (4,4,5.2) circle (0.2);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

编辑:修复了 z 单位。

相关内容