在 pgfplots 中强制网格为正方形

在 pgfplots 中强制网格为正方形

我正在尝试使用 pgfplots 创建一个图,使网格线为正方形,同时保留所有其他标签等。我可以手动设置图的高度和宽度,但我不确定如何正确设置比例以使网格最终为正方形。下面是一个 MWE,其中轴的标签与我想要的一样,但网格是 pgfplots 确定的矩形。

\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    axis lines=middle, xlabel = $t$, ylabel = {},
    xmin = -0, xmax = 8.5, ymin = -0, ymax = 4.5,
    ytick = {1,2,3,4}, %height = 40mm,
    xtick = {1,2,3,...,8}, %width = 80mm,
    grid = both
    ]
    \end{axis}
\end{tikzpicture}
\end{document}

答案1

如果axis equal不是您要找的,那么很有可能是axis equal image

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=middle,
        xlabel = $t$,
        ylabel = {},
        xmin = -0,
        xmax = 8.5,
        ymin = -0,
        ymax = 4.5,
        ytick = {1,2,3,4},
        xtick = {1,2,3,...,8},
        grid = both,
        axis equal image,       % <-- added
    ]
    \end{axis}
\end{tikzpicture}
\end{document}

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

答案2

您需要添加axis equal,axis选项:

\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    axis equal,     % <---
    axis lines=middle, xlabel = $t$, ylabel = {},
    xmin = -0, xmax = 8.5, ymin = -0, ymax = 4.5,
    ytick = {1,...,4}, %height = 40mm,
    xtick = {1,...,8}, %width = 80mm,
    grid = both
    ]
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或更好:

\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    x=1cm, y=1cm,     % <---
    axis lines=middle, xlabel = $t$, ylabel = {},
    xmin = -0, xmax = 8.5, ymin = -0, ymax = 4.5,
    ytick = {1,...,4},      %height = 40mm,
    xtick = {1,...,8}, %width = 80mm,
    grid = both
    ]
    \end{axis}
\end{tikzpicture}
\end{document}

这使:

在此处输入图片描述

相关内容