关于 pgfplot 中的网格线的帮助(版本 3)

关于 pgfplot 中的网格线的帮助(版本 3)

以下显示是带有网格线的笛卡尔平面上的一条直线和一条抛物线。网格线画得很好……但为了绘制网格线,我加入了选项axisxtick={-7,-6,...,7},ytick={-5,-4,...,6},这些选项会打印沿轴的所有数字,这很乱。除了沿 x 轴和沿 y 轴的-3和,我想删除所有刻度标记的标签。(因为我有一个网格,所以我猜刻度标记本身是多余的。)34

我认为打印刻度线时不需要刻度线或标签-334。我可以将坐标(-3,0)(3,0)(0,4)放在节点中并使用选项fill=white。任何美学建议都将不胜感激。

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}

\begin{axis}[grid=both,grid style={line width=.1pt, draw=gray!10},major grid style={line width=.1pt,draw=gray!10}, clip=false,
    xmin=-7,xmax=7,
    ymin=-5,ymax=6,
    axis lines=middle,
    enlargelimits={abs=0.5},
    xtick={-7,-6,...,7},ytick={-5,-4,...,6},
    xticklabel style={fill=none},
    yticklabel style={fill=none},
    extra x ticks={-3,3},
    extra x tick labels={\,$-3$, $3$},
    extra x tick style={x tick label style={font=\tiny,fill=white}},
    extra y ticks={4},
    extra y tick labels={$4$},
    extra y tick style={y tick label style={above left, font=\tiny,fill=white}},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-6.75:1.5] {(4/3) * x + 4} node[anchor=south east,pos=0.1,fill=white,font=\footnotesize]{$y = f(x)$};
\addplot[samples=201,domain=-4.5:4.5] {-(4/9) * x^2 + 4} node[anchor=south west,pos=0.6,fill=white,font=\footnotesize]{$y = g(x)$};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

你可以这样做:

xticklabels={,,,,-3,,}

等等。当然,这是乏味且耗时的。您使用的方法基本上是正确的:仅使用额外的刻度标签来绘制您需要的刻度标签,尽管您不需要\,它们。

如果要将常规标签设为空,则有两种方法可以实现,两种方法都有效(我在我的代码中都使用了它们):

xticklabels={\empty},
yticklabels={,,},

此外,您还可以将样式应用于刻度标签。您可以单独为常规和额外刻度标签设置样式,也可以使用常规样式,yticklabel style={},并将其应用于两者。

输出

图1

代码

\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{calc}

\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,grid style={line width=.1pt, draw=gray!10},major grid style={line width=.1pt,draw=gray!10}, clip=false,
    xmin=-7,xmax=7,
    ymin=-5,ymax=6,
    axis lines=middle,
    enlargelimits={abs=0.5},
    xtick={-7,-6,...,7},
    ytick={-5,-4,...,6},
    xticklabels={\empty},
    yticklabels={,,},
    extra x ticks={-3,3},
    extra x tick labels={$-3$, $3$},
    extra y ticks={4},
    extra y tick labels={$4$},
    yticklabel style={anchor=south east},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-6.75:1.5] {(4/3) * x + 4} node[anchor=south east,pos=0.1,fill=white,font=\footnotesize]{$y = f(x)$};
\addplot[samples=201,domain=-4.5:4.5] {-(4/9) * x^2 + 4} node[anchor=south west,pos=0.6,fill=white,font=\footnotesize]{$y = g(x)$};

\end{axis}
\end{tikzpicture}
\end{document}

答案2

PSTricks 解决方案:

\documentclass{article}

\usepackage{pst-plot}
\psset{algebraic}

\begin{document}

\begin{figure}
 \centering
  \begin{pspicture}(-6,-3.5)(4.9,6.9) % boundry found manually
    % axes
    \psaxes[labels = none]{->}(0,0)(-5.5,-3.5)(4.5,6.5)[$x$,0][$y$,90]
    \uput[300](-3,0){$-3$}
    \uput[230](3,0){$3$}
    \uput[135](0,4){$4$}
    % labels
    \uput[150](-4.5,-2){$y = f(x)$}
    \uput[30](!2 20 9 div){$y = g(x)$}
    % graphs
    \psplot[linecolor = blue]{-5}{1.5}{4/3*x+4}
    \psplot[linecolor = red]{-4}{4}{-4/9*x^2+4}
  \end{pspicture}
 \caption{Add caption here.}
 \label{figure:something}
\end{figure}

\end{document}

输出

答案3

这不是一个优雅的解决方案,但它可能会对你有所帮助:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}

\begin{axis}[grid=major,grid style={line width=.1pt, draw=gray!10},major grid style={line width=.1pt,draw=gray!10}, clip=false,
xmin=-7,xmax=5,
ymin=-5,ymax=6,
axis lines=middle,
enlargelimits={abs=0.5},
xtick=\empty,ytick=\empty,
extra x ticks={-7,-6,...,5},extra y ticks={-5,-4,...,6},
xticklabel style={fill=none},
yticklabel style={fill=none},
extra x tick labels={$\,$,$\,$,$\,$,$\,$,$\quad-3$,$\,$,$\,$,$\,$,$\,$,$\,$,$3\;$},
extra x tick style={x tick label style={font=\tiny}},
extra y tick labels={$\,$,$\,$,$\,$,$\,$,$\,$,$0$,$\,$,$\,$,$\,$,$4$},
extra y tick style={y tick label style={above left, font=\tiny}},
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west},axis equal
]

\addplot[latex-latex,samples=2,domain=-6.75:1.5] {(4/3) * x + 4} node[anchor=south east,pos=0.1,fill=white,font=\footnotesize]{$y = f(x)$};
\addplot[samples=201,domain=-4.5:4.5] {-(4/9) * x^2 + 4} node[anchor=south west,pos=0.6,fill=white,font=\footnotesize]{$y = g(x)$};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案4

这是 Alenanno 建议的另一种代码选项。

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}
\begin{tikzpicture}
\begin{axis}[grid=both,grid style={line width=.1pt, draw=gray!10},major grid style={line width=.1pt,draw=gray!10}, clip=false,
    xmin=-7,xmax=7,
    ymin=-5,ymax=6,
    axis lines=middle,
    enlargelimits={abs=0.5},
    xtick={-7,-6,...,7},ytick={-5,-4,...,6},
    xticklabels={,,,,$-3$,,,,,3,,,,,},yticklabels={,,,,,,,,,4,,},
    xticklabel style={yshift=-2pt, font=\tiny,fill=white},
    yticklabel style={above left, font=\tiny,fill=white},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-6.75:1.5] {(4/3) * x + 4} node[anchor=south east,pos=0.1,fill=white,font=\footnotesize]{$y = f(x)$};
\addplot[samples=201,domain=-4.5:4.5] {-(4/9) * x^2 + 4} node[anchor=south west,pos=0.6,fill=white,font=\footnotesize]{$y = g(x)$};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容