LaTex 用户。
我正在尝试复制下图所示的网格图和一些直线图。如您所见,图表的 x 轴(范围为 0 到 30)已调整,因此 x 轴看起来太宽了。
但是,我正在使用的代码如下
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyheadings}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepgfplotslibrary{fillbetween}
\begin{document}
\setlength{\parindent}{0cm}
%\pagecolor{blue!13}
\fancyhf{}
\fancyhead[C]{\thepage}
\voffset = 0.1cm
\pagestyle{fancy}
\definecolor{web}{RGB}{56, 102, 166}
\setlength{\parindent}{0mm}
\setcounter{page}{1}
\begin{tikzpicture}[middle grid style/.style={lightgray,line width=0.5pt}]
\pgfplotsset{%
% enable layer, needed to draw middle grid below axis
set layers=standard,
% disable ticks
every major tick/.style={draw=none},
every minor tick/.style={draw=none},
}
\begin{axis} [
axis lines = {center},
% set fixed scale to get mm grid
% note: this is for the major grid
x=1cm,
y=1cm,
ylabel = {$y$},
xlabel = {$x$},
ytick distance = 1,
xtick distance = 5,
% number of minor ticks between 2 major ticks
minor x tick num=9,
minor y tick num=9,
ymin=-0.5,
ymax=5.8,
xmin=-0.6,
xmax=30.5,
major grid style={lightgray,thick},
minor grid style={lightgray,very thin},
grid=both,
axis line style={thick},
]
% draw middle grid
\begin{pgfonlayer}{axis grid}
\foreach \x in {-0.5,0.5,...,4.5}{
% \edef-trick, see manual page 541
\edef\temp{\noexpand\draw[middle grid style]
(axis cs:\x,\pgfkeysvalueof{/pgfplots/ymin}) --
(axis cs:\x,\pgfkeysvalueof{/pgfplots/ymax});}
\temp
}
% v-- first, second and last y-positions for middle grid
\foreach \y in {-0.5,0.5,...,10.5}{
\edef\temp{\noexpand\draw[middle grid style]
(axis cs:\pgfkeysvalueof{/pgfplots/xmin},\y) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\y);}
\temp
}
\end{pgfonlayer}
%\addplot [mark = none, domain= 0:10, smooth]{1.1*x};
\node[below] at (-0.2,0) {$O$};
\end{axis}
\end{tikzpicture}
\end{document}
给出 x 轴非常宽的图表,如下所示:
我需要在代码中做哪些更改才能实现我想要的效果?
非常感谢。
答案1
x=1cm
显然是错的,它为每个轴单位提供 1 厘米,因此轴宽变为 30 厘米。如果您想或多或少地精确复制该值,我会设置scale only axis,width=6cm,height=5cm
,并将轴限值更改为xmin=0,xmax=30,ymin=0,ymax=5
。您可以使用shorten >=-3mm
来axis line style
稍微延长轴线。
(屏幕截图没有显示中间的线,但它们在那里,放大 PDF 即可看到。)
\documentclass[]{article}
\usepackage[margin=0.5in, layoutvoffset=0.1cm]{geometry}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepackage{mathtools}
\usepackage{cancel}
%\usepackage{pgfplots} %loaded above
%\usepackage{amsmath} % loaded by mathtools
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
%\usepackage{tikz} % loaded by pgfplots
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyheadings}
\usepackage{bigints}
%\usepackage{color} % loaded by tikz
\usepackage{tcolorbox}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepgfplotslibrary{fillbetween}
\setlength{\parindent}{0cm} % don't you want any indication of new paragraphs?
%\pagecolor{blue!13}
\fancyhf{}
\fancyhead[C]{\thepage}
%\voffset = 0.1cm % I think it's generally better to use the geometry package for all such things
\pagestyle{fancy}
\definecolor{web}{RGB}{56, 102, 166}
%\setlength{\parindent}{0mm} % you did this a few lines ago
%\setcounter{page}{1} %why?
\begin{document}
\begin{tikzpicture}[middle grid style/.style={lightgray,line width=0.5pt}]
\pgfplotsset{%
% enable layer, needed to draw middle grid below axis
set layers=standard,
% disable ticks
every major tick/.style={draw=none},
every minor tick/.style={draw=none},
}
\begin{axis}[
axis lines = {center},
% set width of axis box
scale only axis,
width=6cm,
height=5cm,
ylabel = {$y$},
xlabel = {$x$},
ytick distance = 1,
xtick distance = 5,
% number of minor ticks between 2 major ticks
minor x tick num=9,
minor y tick num=9,
% all axis limits are modified
ymin=0,
ymax=5,
xmin=0,
xmax=30,
major grid style={lightgray,thick},
minor grid style={lightgray,very thin},
grid=both,
axis line style={thick, shorten >=-3mm, line cap=rect},
clip mode=individual, % so that the $O$ node isn't clipped
ylabel style={above left, yshift=3pt},
xlabel style={above right}
]
% draw middle grid
\begin{pgfonlayer}{axis grid}
\foreach \x in {2.5,7.5,...,27.5}{ % <-- modified these values
% \edef-trick, see manual page 541
\edef\temp{\noexpand\draw[middle grid style]
(axis cs:\x,\pgfkeysvalueof{/pgfplots/ymin}) --
(axis cs:\x,\pgfkeysvalueof{/pgfplots/ymax});}
\temp
}
% v-- first, second and last y-positions for middle grid
\foreach \y in {0.5,1.5,...,4.5}{ % <-- modified these as well
\edef\temp{\noexpand\draw[middle grid style]
(axis cs:\pgfkeysvalueof{/pgfplots/xmin},\y) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\y);}
\temp
}
\end{pgfonlayer}
%\addplot [mark = none, domain= 0:10, smooth]{1.1*x};
\node[below] at (-0.2,0) {$O$};
\end{axis}
\end{tikzpicture}
\end{document}