我正在尝试用 Ti 在彩色背景上创建分段函数钾Z 在坐标轴上每 2 个单位有一个刻度标记,但网格每 1 个单位与坐标轴相交。我遇到的问题是,当我尝试为不想显示在网格上的刻度标记标签添加空白时,空白标签的颜色背景会切断网格线。
这是我的 MWE:
\documentclass[12pt]{article}
\usepackage{amsmath, amssymb}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{positioning,calc}
\usepackage{pgfplots}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*}}
\pgfplotsset{holdot/.style={color=black,fill=white,only marks,mark=*}}
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{ExampleFrame}{%
topline=false,
bottomline=false,
leftline=false,
rightline=false,
innertopmargin=20pt,
innerbottommargin=20pt,
innerrightmargin=20pt,
innerleftmargin=20pt,
backgroundcolor=blue!5!white}
\begin{document}
\begin{mdframed}[style=ExampleFrame]
\begin{center}
\begin{tikzpicture}
\begin{axis}[grid=both,
axis lines=middle,
ticklabel style={fill=blue!5!white},
xmin=-6.5,xmax=6.5,
xtick={-6,-5,-4,-3,-2,-1,1,2,3,4,5,6},xticklabels={-6, ,-4, ,-2, , ,2, ,4, ,6},
ymin=-4.5,ymax=4.5,
ytick={-4,-3,-2,-1,1,2,3,4},yticklabels={-4, ,-2, , ,2, ,4},
xlabel=\(x\),ylabel=\(y\),
samples=200]
\addplot[domain=-6:-2,thick] {x+3};
\addplot[domain=-2:1,thick] {2*x+2};
\addplot[domain=1:3,thick] {-2*x+6};
\addplot[domain=3:4,thick] {-4*x+15};
\addplot[domain=4:6,thick] {-1};
\addplot[holdot] coordinates{(-2,1)(3,3)};
\addplot[soldot] coordinates{(-6,-3)(-2,-2)(3,0)(6,-1)};
\end{axis}
\end{tikzpicture}
\end{center}
\end{mdframed}
\end{document}
从而产生了这个。
我该如何修复空白标签以使它们不阻挡网格?
答案1
您可以简单地仅给出您想要的标记点并制作剩余的标记点,这样minor tick
您就不需要跳过您不想要的标签。
一个细节是,如果您有一个缺少第二个参数的颜色规范,那么blue!10
就会white
自动假定。
此外,200 个线条样本也显得过多了。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*},
holdot/.style={color=black,fill=white,only marks,mark=*},
compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,
axis lines=middle,
ticklabel style={fill=blue!5!white},
xmin=-6.5,xmax=6.5,
ymin=-4.5,ymax=4.5,
xtick={-6,-4,-2,2,4,6}, %<--
ytick={-4,-2,2,4}, %<--
minor tick = {-5,-3,...,5}, %<--
xlabel=\(x\),ylabel=\(y\),
samples=200]
\addplot[domain=-6:-2,thick] {x+3};
\addplot[domain=-2:1,thick] {2*x+2};
\addplot[domain=1:3,thick] {-2*x+6};
\addplot[domain=3:4,thick] {-4*x+15};
\addplot[domain=4:6,thick] {-1};
\addplot[holdot] coordinates{(-2,1)(3,3)};
\addplot[soldot] coordinates{(-6,-3)(-2,-2)(3,0)(6,-1)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
PSTricks 解决方案:
\documentclass{article}
\usepackage{pst-plot}
\begin{document}
\psset{unit = 0.87} % scaling to avoid `overfull \hbox` warning
\begin{pspicture}(-6.4,-4.5)(6.92,4.95) % boundry found manually
% coordinate grid
\psaxes[
labels = none,
tickcolor = gray!50,
tickwidth = 0.5pt,
xticksize = -4 4,
yticksize = -6 6
]{->}(0,0)(-6.4,-4.5)(6.5,4.5)
% axes
\psaxes[
Dx = 2,
Dy = 2
]{->}(0,0)(-6.4,-4.5)(6.5,4.5)[$x$,0][$y$,90]
% data points and line segments
\psline{*-o}(-6,-3)(-2,1)
\psline{*-*}(-2,-2)(1,4)(3,0)
\psline{o-*}(3,3)(4,-1)(6,-1)
\end{pspicture}
\end{document}