我正在尝试绘制如下图所示的图表:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
axis lines=center,
width=25cm,
height=10cm,
xmin=0,
ymin=0,
xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},
ytick={0,5,10},
clip=false,
xmajorgrids,
ymajorgrids,
grid style=dashed,
%%%%%%% FIRST ATTEMPT
%axis equal image,
%%%%%%% SECOND ATTEMPT
%x=.5cm,
%y=.5cm,
%%%%%%% THIRD ATTEMPT
%width=24cm,
%height=10cm,
]
\addplot coordinates {(0,0)(5,5)(8,10)(24,0)};
\end{axis}
\end{tikzpicture}
\end{document}
y
由于-轴上的“点?”比-轴上的“点?”少x
,因此图形具有矩形网格。我想用方格画出图例如:
我已经尝试了几种方法这里和这里但没有成功。代码中已注释了这些尝试。
我们如何才能达到预期的效果?缩放的公式是什么?
答案1
尝试一下unit vector ratio*={5 1 1}
。注意,这会覆盖height=10cm
。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
axis lines=center,
width=25cm,
height=10cm,
unit vector ratio*={5 1 1}, % <<< added
xmin=0,
ymin=0,
xtick={0,1,...,24},
ytick={0,5,10},
clip=false,
xmajorgrids,
ymajorgrids,
grid style=dashed,
%%%%%%% FIRST ATTEMPT
%axis equal image,
%%%%%%% SECOND ATTEMPT
%x=.5cm,
%y=.5cm,
%%%%%%% THIRD ATTEMPT
%width=24cm,
%height=10cm,
]
\addplot coordinates {(0,0)(5,5)(8,10)(24,0)};
\end{axis}
\end{tikzpicture}
\end{document}