![如何用对数 x 轴绘制 sin(1/x)?](https://linux22.com/image/255621/%E5%A6%82%E4%BD%95%E7%94%A8%E5%AF%B9%E6%95%B0%20x%20%E8%BD%B4%E7%BB%98%E5%88%B6%20sin(1%2Fx)%EF%BC%9F.png)
我想绘制函数图f(x) = sin(1/x)
,它应该看起来像这样. 我目前得到的信息是:
您可能会注意到,由于轴的原因,我的图像看起来不如链接的图像好。
我怎样才能得到我的图的对数轴?
为什么它目前会产生对数轴错误?
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, calc, intersections}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
width=15cm, height=15cm, % size of the image
grid = major,
grid style={dashed, gray!30},
%xmode=log,log basis x=10, % <- doesn't work:
% ! Missing number, treated as zero.
% <to be read again>
% p
% l.33 ..., red, thick,samples=500] {sin(deg(1/x))};
%
% ?
% ! Emergency stop.
% <to be read again>
% p
% l.33 ..., red, thick,samples=500] {sin(deg(1/x))};
%
% ! ==> Fatal error occurred, no output PDF file produced!
%ymode=log,log basis y=10,
xmin=-0.7000, % start the diagram at this x-coordinate
xmax=-0.0062, % end the diagram at this x-coordinate
ymin=-1, % start the diagram at this y-coordinate
ymax= 1, % end the diagram at this y-coordinate
axis background/.style={fill=white},
ylabel=y,
xlabel=x,
%xticklabels={,,},
%yticklabels={,,},
tick align=outside,
tension=0.08]
\addplot[domain=-0.700:-0.0062, red, thick,samples=500] {sin(deg(1/x))};
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
如果用对数 y 轴绘制它,它看起来......嗯......我们称它为有趣。
该图的最新 LaTeX 代码可在以下网址找到:GitHub。
答案1
问题在于,对于负数,对数不是实数,因此您必须使用一些技巧:为正值域绘制函数,但翻转 x 轴的方向和刻度标签的符号:
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}[
xmode=log, % Logarithmic x axis
xmin=0.01, xmax=1, % Positive domain...
x dir=reverse, % ... but increasing to the left
xticklabel=\pgfmathparse{-exp(\tick)}\pgfmathprintnumber{\pgfmathresult}, % Reverse the sign of the labels
xticklabel style={/pgf/number format/.cd,fixed}, % Use fixed point notation
width=15cm, height=8cm, % size of the image
grid = major,
grid style={gray!30},
ymin=-1, % start the diagram at this y-coordinate
ymax= 1, % end the diagram at this y-coordinate
axis background/.style={fill=white},
ylabel=y,
xlabel=x,
tick align=outside
]
\addplot[domain=0.01:1, red, thick,samples=2000] {-sin(deg(1/(x)))};
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
答案2
运行xelatex
\documentclass{article}
\usepackage{pst-plot}
\pagestyle{empty}
\begin{document}
\psset{xunit=10,yunit=3}
\begin{pspicture}(0.05,-1.1)(-1.05,1.1)
\psaxes[axesstyle=frame,ticksize=0 5pt,Dx=0.2,Dy=0.5,ylabelPos=right]{->}(0,0)(0,-1.25)(-1.1,1.25)[$\log(x)$,180][$sin(1/\log(x))$,90]
\psplot[plotpoints=10000,linecolor=red,algebraic]{-0.1}{-0.001}{sin(1/(log(1-x)))}
\psplot[plotpoints=1000,linecolor=red,algebraic]{-0.99}{-0.1}{sin(1/(log(1-x)))}
\end{pspicture}
\end{document}