我希望能够在 Tikz 中绘制标准函数,用于我正在编写的 LaTeX 文档。
到目前为止,我尝试过并使用了这段代码
\begin{tikzpicture}
\draw[->] (-5, 0) -- (5, 0) node[right] {$\vu*{x}$};
\draw[->] (0, -3) -- (0, 5) node[above] {$\vu*{y}$};
\draw[scale=0.5, domain=-3:3, smooth, variable=\x, red] plot ({\x}, {3});
\end{tikzpicture}
这里我只是想绘制一个常数函数。
现在,这很糟糕,但可以起作用(只需进行少量修改)。
我问你这个问题:我怎样才能在轴上添加刻度标签?在 $x$ 和 $y$ 轴上,以单位步长为单位?比如在轴上(负数也是)添加 $0、1、2、3 等等?
另外,如何在绘制的函数中添加点线或虚线末端?
太感谢了!
添加
好的,通过添加解决了第二个问题:
\draw[scale=0.5, domain=-5.5:-4.5, thick, dashed, variable=\x, black] plot ({\x}, {3});
\draw[scale=0.5, domain=4.5:5.5, thick, dashed, variable=\x, black] plot ({\x}, {3});
添加 2
我的文件序言:
\documentclass[a4paper, 14pt]{extarticle}
\usepackage{geometry}
\geometry{total={173mm, 257mm}, left=23mm, top=23mm, right=23mm, bottom=23mm}
\usepackage{amsmath}
%\usepackage{newtxtext}
\usepackage[bb=stixtwo]{mathalpha}
\usepackage{concmath}
\usepackage[usenames,dvipsnames]{color}
\usepackage{graphicx}
\usepackage{pstricks}
\usepackage{tikz}
\usetikzlibrary {datavisualization.formats.functions}
\usepackage{pspicture}
\usepackage{physics}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{wasysym}
\usepackage{tabularx}
\usepackage{mathtools}
\usepackage{enumerate}
\usepackage{verbatim}
\usepackage{tcolorbox}
\pagenumbering{gobble}
答案1
像这样(具有 2 个以上功能):
代码:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[gray!20](-4,-4) grid (4,4); % grid
% function(s) to plot in the next lines
\draw[magenta,domain=-4:4,line width=2pt] plot (\x,3);
\draw[smooth,cyan,domain=-pi:pi,line width=2pt] plot (\x,{sin(3*\x r)+\x});
\draw[smooth,dotted,domain=-pi:pi,line width=1pt] plot (\x,\x);
% x & y axis plus scale
\foreach \x in {-4,-3,-2,-1,1,2,3,4}{
\draw (\x,.1)--(\x,-.1) node[below]() {\footnotesize $\x$};
\draw (.1,\x)--(-.1,\x) node[left,fill=white]() {\footnotesize $\x$};
}
\draw[->,line width=.5pt] (-4,0)-- (4.5,0) node[below]() {$x$};
\draw[->,line width=.5pt] (0,-4)-- (0,4.5) node[right]() {$y$};
\node at(.2,-.2) () {\footnotesize O};
\end{tikzpicture}
\end{document}
答案2
用 puretikz
可以是这样的:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
backgrounds}
\begin{document}
\begin{tikzpicture}[> = Straight Barb,
ticklabel/.style = {fill=white, font=\footnotesize, inner sep=2pt, anchor=#1}
]
\draw[->] (-5, 0) -- (5, 0) node[right] {$X$};
\draw[->] (0, -2) -- (0, 4) node[above] {$Y$};
\foreach \i in {-4, -3,...,4}
\draw (\i,0.1) -- ++ (0,-0.2) node[ticklabel=north] {\i};
\foreach \i in {-1, 1,2,3}
\draw (0.1,\i) -- ++ (-0.2,0) node[ticklabel=east] {\i};
\scoped[on background layer]
\draw[domain=-4:4, samples=2, thick] plot (\x, 3);
\draw[domain=-5:-4, samples=2, thick, dashed] plot (\x, 3);
\draw[domain= 4: 5, samples=2, thick, dashed] plot (\x, 3);
\end{tikzpicture}
\end{document}
附录:
然而,对于这样的图表通常使用“pgfplots包:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
axis on top,
label style = {anchor=north east},
xlabel = $x$, ylabel = $y$,
xmin=-5, xmax = 5,
ymin=-2, ymax = 5,
ytick = {-1,1,2,3,4},
ticklabel style = {font=\footnotesize, fill=white, inner sep=2pt},
samples=2, no marks,
every axis plot post/.append style={very thick, color=blue}
]
\addplot [domain = -5:-3, dashed] {3};
\addplot [domain = -3:3] {3};
\addplot [domain = 3:5, dashed] {3};
\end{axis}
\end{tikzpicture}
\end{document}
我把为轴编写正确的符号的工作留给了你(抱歉,你没有提供 MWE:最小工作示例,以便我可以看到你的文档序言)。