![如何控制网格线穿透轴线](https://linux22.com/image/382732/%E5%A6%82%E4%BD%95%E6%8E%A7%E5%88%B6%E7%BD%91%E6%A0%BC%E7%BA%BF%E7%A9%BF%E9%80%8F%E8%BD%B4%E7%BA%BF.png)
此图上的网格线突出于第一象限,看起来不太好看。
我被建议使用
xticklabel style={
%xshift=10pt,
fill=white
},
这对于更精细缩放的轴非常有效,但我所拥有的轴更加分散,因此存在问题。
有人能提出解决办法吗?
我已附上代码和输出。
非常感谢
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyhdr}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{color,xcolor}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepackage{graphicx}
\usetikzlibrary{arrows}
\usepackage{polynom}
\usepackage{flexisym}
\usepackage{wallpaper}
\usepackage{blkarray}
\usepackage{caption}
\usepgfplotslibrary{fillbetween}
\usepgfplotslibrary{statistics}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
grid=major,
%ticks=none,
xmin=-1,
xmax=32,
ymin=-3,
ymax=52.5,
minor tick num=4,
grid=both,
grid style={line width=.1pt, draw=gray!30},
major grid style={line width=.2pt,draw=gray!60},
xtick={0,5,10,15,20,25,30},
ytick={0,10,20,30,40,50},
height=10cm,
width=15cm,
axis line style={shorten >=-10pt, shorten <=-10pt},
ylabel style={
anchor=south,
at={(ticklabel* cs:1.0)},
yshift=10pt
},
ylabel=$y$,
xlabel style={
anchor=west,
at={(ticklabel* cs:1.0)},
xshift=10pt
},
xlabel=$x$,
yticklabel style={
fill=white,
%yshift=10pt,
},
xticklabel style={
%xshift=10pt,
fill=white
},
]
\node[below] at (axis cs:-0.6,0.08) {$O$};
%
%\addplot[thick,samples=100,domain=0:25] {2*x};
\addplot[thick,samples=100,domain=0:25] {50-2*x};
\addplot[thick,samples=100,domain=0:30] {10};
\addplot[thick,samples=100,domain=10:24] {4*x-40};
%
\addplot[name path=func0,color=black,samples=100,opacity=0,domain=5:12.5] {10};
%\addplot[name path=func1,color=black,samples=100,opacity=0,domain=5:12.5] {2*x};
%
\addplot[name path=func3,color=black,samples=100,opacity=0,domain=5:15] {50-2*x};
\addplot[name path=func4,color=black,samples=100,opacity=0,domain=12.5:15] {4*x-40};
%
\draw[thick] (axis cs:5,0)--(axis cs:5,50);
%
\addplot fill between[
of = func3 and func0,
soft clip={domain=5:12.5},
every even segment/.style = {gray,opacity=.4}
];
%
\addplot fill between[
of = func3 and func4,
soft clip={domain=12.5:15},
every even segment/.style = {gray,opacity=.4}
];
%
\node[above] at (axis cs:4.4,10.3) {$A$};
\node[above] at (axis cs:5.7,40.3) {$B$};
\node[right] at (axis cs:15.3,20) {$C$};
\node[below] at (axis cs:13,10) {$D$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
如果您仅设置xmin=0
和ymin=0
,网格线将不会超出轴。更新:删除了不相关的样式并添加clip mode=individual
以及将样本减少到 2 个,这两项都是 Torbjørn T 建议的。
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[clip mode=individual, %<- big thanks to Torbjørn T.
axis lines=middle,
grid=major,
%ticks=none,
xmin=0,
xmax=32,
ymin=0,
ymax=52.5,
minor tick num=4,
grid=both,
grid style={line width=.1pt, draw=gray!30},
major grid style={line width=.2pt,draw=gray!60},
xtick={0,5,10,15,20,25,30},
ytick={0,10,20,30,40,50},
height=10cm,
width=15cm,
axis line style={shorten >=-10pt, shorten <=-10pt},
ylabel style={
anchor=south,
at={(ticklabel* cs:1.0)},
yshift=10pt
},
ylabel=$y$,
xlabel style={
anchor=west,
at={(ticklabel* cs:1.0)},
xshift=10pt
},
xlabel=$x$,
yticklabel style={
fill=white,
%yshift=10pt,
},
xticklabel style={
%xshift=10pt,
fill=white
},samples=2 %<- big thanks Torbjørn T.
]
\node[below] at (axis cs:-0.6,0.08) {$O$};
%
%\addplot[thick,samples=100,domain=0:25] {2*x};
\addplot[thick,domain=0:25] {50-2*x};
\addplot[thick,domain=0:30] {10};
\addplot[thick,domain=10:24] {4*x-40};
%
\addplot[name path=func0,color=black,opacity=0,domain=5:12.5] {10};
%\addplot[name path=func1,color=black,opacity=0,domain=5:12.5] {2*x};
%
\addplot[name path=func3,color=black,opacity=0,domain=5:15] {50-2*x};
\addplot[name path=func4,color=black,opacity=0,domain=12.5:15] {4*x-40};
%
\draw[thick] (axis cs:5,0)--(axis cs:5,50);
%
\addplot fill between[
of = func3 and func0,
soft clip={domain=5:12.5},
every even segment/.style = {gray,opacity=.4}
];
%
\addplot fill between[
of = func3 and func4,
soft clip={domain=12.5:15},
every even segment/.style = {gray,opacity=.4}
];
%
\node[above] at (axis cs:4.4,10.3) {$A$};
\node[above] at (axis cs:5.7,40.3) {$B$};
\node[right] at (axis cs:15.3,20) {$C$};
\node[below] at (axis cs:13,10) {$D$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}