使用 Tikz/PgfPlots 绘制的立方体看起来沿 X 轴拉伸

使用 Tikz/PgfPlots 绘制的立方体看起来沿 X 轴拉伸

我正在尝试使用 Tikz 绘制立方体结构(面心立方晶体结构)。我正在使用 pgfplotsaxis cs函数进行 3D 投影。但结果看起来像沿 x 轴拉伸的矩形。

结果如下

pdf

以下是代码:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,arrows,positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}[
  vector/.style={thick,black,>=stealth,->},
  atom/.style={blue}
  ]
  \begin{axis}[
    axis lines=none,
    xmin=-0.1,
    xmax=1.1,
    ymin=-0.1,
    ymax=1.1,
    zmin=-0.1,
    zmax=1.1,
    xtick=\empty,
    ytick=\empty,
    ztick=\empty
    ]
    \coordinate (A0) at (axis cs:0,0,0);
    \coordinate (A1) at (axis cs:1,0,0);
    \coordinate (A2) at (axis cs:0,1,0);
    \coordinate (A3) at (axis cs:0,0,1);

    \coordinate (A4) at (axis cs:1,1,0);
    \coordinate (A5) at (axis cs:1,0,1);
    \coordinate (A6) at (axis cs:0,1,1);
    \coordinate (A7) at (axis cs:1,1,1);

    \coordinate (A8) at (axis cs:0.5,0.5,0);
    \coordinate (A9) at (axis cs:0.5,0,0.5);
    \coordinate (A10) at (axis cs:0,0.5,0.5);

    \coordinate (A11) at (axis cs:0.5,0.5,1);
    \coordinate (A12) at (axis cs:0.5,1,0.5);
    \coordinate (A13) at (axis cs:1,0.5,0.5);

    \draw[black!25] (A0) -- (A8) -- (A4);
    \draw[black!25] (A4) -- (A12) -- (A6);
    \draw[black!25] (A0) -- (A10) -- (A6);
    \draw[black!25] (A1) -- (A8) -- (A2);
    \draw[black!25] (A2) -- (A10) -- (A3);
    \draw[black!25] (A7) -- (A12) -- (A2);
    \draw[black] (A1) -- (A13) -- (A7);
    \draw[black] (A3) -- (A11) -- (A7);
    \draw[black] (A5) -- (A13) -- (A4);
    \draw[black] (A6) -- (A11) -- (A5);
    \draw[black] (A5) -- (A9) -- (A0);
    \draw[black] (A0) -- (A1);
    \draw[black] (A0) -- (A2);
    \draw[black] (A0) -- (A3);
    \draw[black] (A0) -- (A3);
    \draw[black] (A3) -- (A5);
    \draw[black] (A3) -- (A6);
    \draw[black] (A6) -- (A2);
    \draw[black] (A5) -- (A1);
    \draw[black] (A7) -- (A4);
    \draw[black] (A5) -- (A7);
    \draw[black] (A1) -- (A4);
    \draw[black] (A6) -- (A7);
    \draw[black] (A2) -- (A4);
    \fill[blue] (A8) circle (5pt);
    \draw[vector] (A0) -- (A8);
    \fill[blue] (A10) circle (5pt);
    \draw[vector] (A0) -- (A10);
    \draw[black] (A3) -- (A9) -- (A1);
    \fill[atom] (A1) circle (5pt);
    \fill[atom] (A2) circle (5pt);
    \fill[atom] (A3) circle (5pt);
    \fill[atom] (A4) circle (5pt);
    \fill[atom] (A5) circle (5pt);
    \fill[atom] (A6) circle (5pt);
    \fill[atom] (A7) circle (5pt);
    \fill[atom] (A9) circle (5pt);
    \fill[atom] (A11) circle (5pt);
    \fill[atom] (A12) circle (5pt);
    \fill[atom] (A13) circle (5pt);
    \draw[vector] (A0) -- (A9);
    \fill[blue] (A0) circle (5pt);
  \end{axis}
\end{tikzpicture}
\end{document}

是什么原因导致拉伸?我怎样才能将其变成完美的立方体?

解决方案:设置axis equal=true

答案1

您不需要使用pgfplots,只需(x,y,z)使用坐标。它们之间的关系可以在tikzpicture选项中修复。下图使用x=2cm,y=2cm,z=1cm

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,arrows,positioning}

\begin{document}
\begin{tikzpicture}[
  vector/.style={thick,black,>=stealth,->},
  atom/.style={blue}, x=2cm,y=2cm,z=1cm
  ]
    \coordinate (A0) at (0,0,0);
    \coordinate (A1) at (1,0,0);
    \coordinate (A2) at (0,1,0);
    \coordinate (A3) at (0,0,1);

    \coordinate (A4) at (1,1,0);
    \coordinate (A5) at (1,0,1);
    \coordinate (A6) at (0,1,1);
    \coordinate (A7) at (1,1,1);

    \coordinate (A8) at (0.5,0.5,0);
    \coordinate (A9) at (0.5,0,0.5);
    \coordinate (A10) at (0,0.5,0.5);

    \coordinate (A11) at (0.5,0.5,1);
    \coordinate (A12) at (0.5,1,0.5);
    \coordinate (A13) at (1,0.5,0.5);

    \draw[black!25] (A0) -- (A8) -- (A4);
    \draw[black!25] (A4) -- (A12) -- (A6);
    \draw[black!25] (A0) -- (A10) -- (A6);
    \draw[black!25] (A1) -- (A8) -- (A2);
    \draw[black!25] (A2) -- (A10) -- (A3);
    \draw[black!25] (A7) -- (A12) -- (A2);
    \draw[black] (A1) -- (A13) -- (A7);
    \draw[black] (A3) -- (A11) -- (A7);
    \draw[black] (A5) -- (A13) -- (A4);
    \draw[black] (A6) -- (A11) -- (A5);
    \draw[black] (A5) -- (A9) -- (A0);
    \draw[black] (A0) -- (A1);
    \draw[black] (A0) -- (A2);
    \draw[black] (A0) -- (A3);
    \draw[black] (A0) -- (A3);
    \draw[black] (A3) -- (A5);
    \draw[black] (A3) -- (A6);
    \draw[black] (A6) -- (A2);
    \draw[black] (A5) -- (A1);
    \draw[black] (A7) -- (A4);
    \draw[black] (A5) -- (A7);
    \draw[black] (A1) -- (A4);
    \draw[black] (A6) -- (A7);
    \draw[black] (A2) -- (A4);
    \fill[blue] (A8) circle (5pt);
    \draw[vector] (A0) -- (A8);
    \fill[blue] (A10) circle (5pt);
    \draw[vector] (A0) -- (A10);
    \draw[black] (A3) -- (A9) -- (A1);
    \fill[atom] (A1) circle (5pt);
    \fill[atom] (A2) circle (5pt);
    \fill[atom] (A3) circle (5pt);
    \fill[atom] (A4) circle (5pt);
    \fill[atom] (A5) circle (5pt);
    \fill[atom] (A6) circle (5pt);
    \fill[atom] (A7) circle (5pt);
    \fill[atom] (A9) circle (5pt);
    \fill[atom] (A11) circle (5pt);
    \fill[atom] (A12) circle (5pt);
    \fill[atom] (A13) circle (5pt);
    \draw[vector] (A0) -- (A9);
    \fill[blue] (A0) circle (5pt);
\end{tikzpicture}
\end{document}

答案2

我重新检查了 pgfplots 手册,并axis equal=true在轴选项中设置解决了该问题。

相关内容