我昨天尝试绘制函数f(x)=1/x , 1<=x<=2 and f(x)=x , x<=0
。我使用的是 pgf 图,但代码不起作用,而且由于它让我很烦,所以我就删除了它。
因此我不知道该如何绘制这个。它有一些跳跃不连续性,也许我需要添加一些包才能工作。无论如何,有什么想法吗?
编辑:
\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-1.7066666666666666,0.0) -- (2.56888888888889,0.0);
\foreach \x in {-1.5,-1.0,-0.5,0.5,1.0,1.5,2.0,2.5}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below]{\footnotesize $\x$};
\draw[->,color=black] (0.0,-1.3644444444444437) -- (0.0,1.924444444444442);
\foreach \y in {-1.0,-0.5,0.5,1.0,1.5}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-1.7066666666666666,-1.3644444444444437) rectangle (2.56888888888889,1.924444444444442);
\draw[smooth,samples=100,domain=1.0000037066666696:1.9999985707336516] plot(\x,{\Alpha\nu[1.0*≤*(\x)*≤*2.0,1.0/x]});
\draw[smooth,samples=100,domain=-1.7066666666666666:-1.7066666650448171E-6] plot(\x,{\Alpha\nu[x*≤*0.0,x]});
\begin{scriptsize}
\draw[color=black] (-2.1511111111111108,-2.1733333333333316) node {$g$};
\end{scriptsize}
\end{tikzpicture}
\end{document}
这就是代码,我不记得我在图中插入的具体命令...并且由于某些原因,我似乎无法在这里正确获取代码...我一定是犯了一些错误..如果有人可以纠正它..我将不胜感激。
答案1
运行xelatex
:
\documentclass{article}
\usepackage{pst-plot}
\begin{document}
\psset{unit=2}
\begin{pspicture}(-2,-1.5)(3,2.5)
\psaxes[Dx=0.5,Dy=0.5]{->}(0,0)(-1.75,-1.25)(2.5,2)
\psplot[linecolor=red,linewidth=1.5pt,algebraic]{1}{2}{1/x}
\psplot[linecolor=blue,linewidth=1.5pt,algebraic]{-1}{0}{x}
\end{pspicture}
\end{document}
答案2
和pgfplots
:
\documentclass[a4paper,12pt, border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}%
\begin{tikzpicture}
\begin{axis}[
axis lines*=middle,
no markers,
ymin=-2, ymax=2,
enlargelimits={abs=1},
axis line style={latex-latex},
xtick={-2,-1,1,2},
ytick={-2,-1,1,2}
]
\addplot+ [thick, samples=50, smooth,domain=1:2,blue] {1/x};
\addplot+ [thick, samples=50, smooth,domain=-2:0,olive] {x};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
只是为了好玩tikz
。看看pgfmanual
和教程(前几章)。它包含您需要的所有信息。
\documentclass[tikz, border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[>=latex]
% axes
\draw [<->] (0,-2.5) -- ++(0,5);
\draw [<->] (-2.5,0) -- ++(5,0);
\foreach \x in {-2,-1,1,2} {
\draw (\x,.125) -- ++(0,-.25) node [below] {\scriptsize \x};
\draw (.125,\x) -- ++(-.25,0) node [left] {\scriptsize \x};
}
% plots
\draw [red, domain=1:2] plot ({\x}, {1/\x});
\draw [blue, domain=-2:0] plot ({\x}, {\x});
\end{tikzpicture}
\end{document}