如何在 TeX 中创建方形图?

如何在 TeX 中创建方形图?

我想在 TeX 中创建一个正方形图,以表示无穷范数的单位球。我正在尝试使用 TikZ 包,但在使用命令时遇到了麻烦\draw。我想创建类似的东西。

在此处输入图片描述

非常感谢您的任何建议!

答案1

这相当容易。

让自己成为 faboulusTikZ 和 PGF 手册和教程并按照第 30 页至第 43 页的说明进行操作,就像我做的那样。

编辑放置标签多于矩形。

\documentclass[11pt]{article}

\usepackage{tikz}

\begin{document}
\noindent%
%% Manual page 29
\begin{tikzpicture}[scale=4.5]
  %% First, the axis, from -1.4 to 1.4 in both directions
  %% Manual page 30, top
  \draw (-1.4, 0) -- (1.4, 0);
  \draw (0, -1.4) -- (0, 1.4);
  %% 
  %% Draw a grid
  %% Manual page 33
  \draw[ultra thin, gray, step=.1cm] (-1.3,-1.3) grid (1.3,1.3);
  %% 
  %% Finally, draw the red rectangle from -1,-1 to 1,1
  %% Manual page 32
  \draw[ultra thick, red] (-1, -1) rectangle (1, 1);
  %% 
  %% Place the labels
  %% Manual 43
  \foreach \x/\xtext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1}
  \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};
  \foreach \y/\ytext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1}
  \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\ytext$};
\end{tikzpicture}
\end{document}

正如 Stefan 所要求的结果---这里是:

在此处输入图片描述

答案2

对于图表来说,最好使用它,pgfplots因为它是专门为绘图而设计的:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
        axis y line=center,
        axis x line=middle, 
        xmin=-1.5,
        xmax=1.5,
        ymin=-1.5,
        ymax=1.5,
        grid=both,
        minor tick num=4,
        axis equal image,
]
    \addplot[red, ultra thick] coordinates {(-1,1) (1,1) (1,-1) (-1,-1) (-1,1)};
\end{axis}
\end{tikzpicture}
\end{document}

答案3

我在这里介绍的只是“稍微改进”的版本Peter Grill 的回答考虑到我在在 Peter 的回答下评论还有更多内容来展示该pgfplots包的更多可能性...

请查看代码中的注释以了解更多详细信息。

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher so you don't have to put `axis cs:'
        % in front of every TikZ (not PGFPlots) coordinate
        % (in this example the coordinates of the rectangle)
        compat=1.11,
        % declare a variable to only have one place where to change the
        % symmetric axis limits
        /tikz/declare function={
            AxisLimit=1.5;
        },
        % define a custom layer set, so the *axis labels* are drawn on top of
        % the draw/plot commands, which otherwise would be partially hidden
        % for this example
        % (I just used the default set and moved `axis tick labels' after `main')
        layers/tick labels on top/.define layer set={
            axis background,axis grid,axis ticks,axis lines,
            main,axis tick labels,axis descriptions,axis foreground
        }{/pgfplots/layers/standard},
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % don't use a boxed axis but centered lines
            % (`center' is an alias for `middle')
            axis lines=middle,
            % apply the axis limits with the help of the defined variable
            xmin=-AxisLimit,    xmax=AxisLimit,
            ymin=-AxisLimit,    ymax=AxisLimit,
            % to have equal vector length for both axis ...
            axis equal image=true,
            % show some minor ticks
            minor tick num=4,
            % show a grid for both of the ticks (major and minor)
            grid=both,
            % apply the above created layer set
            set layers=tick labels on top,
            % in case that only isn't enough you could modify the style of the
            % tick labels to have a white background
            % (which in addition could be a bit transparent)
            tick label style={
                fill=white,
                fill opacity=0.75,
                % (but of course the text should not be transparent)
                text opacity=1,
            },
        ]
            % you could either draw a rectangle using a TikZ command ...
            \draw [
                blue,
                ultra thick,
            ] (-1,-1) rectangle (1,1);

            % ... or by using a PGFPlots command
            \addplot [
                mark=none,
                red,
                thick,
            ] coordinates {
                (-1,-1) (1,-1) (1,1) (-1,1)
            }
                % use this TikZ command to connect the last with the first
                % path element
                -- cycle;
        \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案4

这是使用 的另一种解决方案pgfmath。只是为了好玩,并更好地理解单位球。这些值是通过 -循环和 计算的evaluateforeachpgfmathparse函数int()用于从结果中去除小数值。

\documentclass[tikz, border=5mm]{standalone}

\begin{document}
  \begin{tikzpicture}
    \draw [gray!50!white, step=.2] (-1.3,-1.3) grid (1.3,1.3);
    \draw [thick, ->] (0,-1.4) -- (0,1.4);
    \draw [thick, ->] (-1.4,0) -- (1.4,0);

    \draw [red, ultra thick] (-1,-1) rectangle (1,1);

    \foreach [evaluate=\a as \x using cos(\a),evaluate=\a as \y using sin(\a)] \a in {0,90,180,270} %
        \node [circle, fill, inner sep=1pt, label={-45:\pgfmathparse{int(\x ^2+\y^2)}\pgfmathresult}] at (\x, \y) {};
  \end{tikzpicture}
\end{document}

单位球示例

相关内容