我有一张抛物线图。为什么上面排版有“*10^{4}”?!如何将“20,000”打印为沿 x 轴的刻度标记?我试过了,xticklabels={$20,000$}
但代码无法编译。
\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=3in, height=3in, axis lines=middle, clip=false,
axis lines=middle, clip=false,
xmin=-1000,xmax=37320.5,
ymin=-200000,ymax=725000,
restrict y to domain=-200000:725000,
xtick={20000},ytick={\empty},
ticklabel style={font=\scriptsize},
xticklabels={$20000$},
axis line style={latex-latex},
xlabel=$x$,ylabel=$y$,
axis line style={shorten >=-12.5pt, shorten <=-12.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west}
]
\addplot[samples=501, domain=-1000:37320.5] {-0.003*x^2 + 120*x - 500000};
%P = (25000, 625000) is a point on the parabola. The slope of the tangent line at P
%is -30. An equation for the tangent line at P is y = -30x + 1,375,000.
\coordinate (P) at (25000, 625000);
\coordinate (Q) at (137500/3, 0);
\end{axis}
%A "pin" is drawn to the parabola.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (P) -- ($(P)!0.75cm!90:(Q)$);
\node[anchor=west, inner sep=0, font=\footnotesize] at ($(P)!0.75cm!90:(Q)$){\makebox[0pt][l]{$y=P(x)$}};
\end{tikzpicture}
\end{document}
答案1
正如符号 1 在问题下方的评论中所述,关键是设置scaled ticks=false
,并且 PGFPlots 手册指出该关键:
允许从刻度标签中抽取常见指数线性轴。例如,如果您有刻度标签 20000;40000 和 60000,您可能希望节省一些空间并写入 2;4;6 以及单独的因子 '$\cdot 10^4$'。使用
scaled ticks=true
启用此功能。在 的情况下true
,如果数据范围太大或太小,将触发刻度缩放。
而且由于数字已经按照您想要的方式显示出来,您无需再进行xticklabels
任何说明。
(请注意,我对代码还做了一些其他的小改动,但我没有在这里评论。)
% used PGFPlots v1.14
\documentclass[border=15pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=3in,
height=3in,
axis lines=middle,
xmin=-1000,
xmax=37320.5,
ymin=-200000,
ymax=725000,
xtick={20000},
% ---------------------------------------------------------------------
% you don't want the ticks/tick labels to be scaled
scaled ticks=false,
% % and the tick labels are shown by default the way you want them,
% % so you don't need to specify them explicitely
% xticklabels={$20000$},
% ---------------------------------------------------------------------
ytick={\empty},
ticklabel style={font=\scriptsize},
xlabel=$x$,
ylabel=$y$,
axis line style={
latex-latex,
shorten >=-12.5pt,
shorten <=-12.5pt,
},
xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west},
]
\addplot[samples=51,smooth,domain=-1000:37320.5] {-0.003*x^2 + 120*x - 500000};
%P = (25000, 625000) is a point on the parabola. The slope of the tangent line at P
%is -30. An equation for the tangent line at P is y = -30x + 1,375,000.
\coordinate (P) at (25000, 625000);
\coordinate (Q) at (137500/3, 0);
\end{axis}
%A "pin" is drawn to the parabola.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (P) -- ($(P)!0.75cm!90:(Q)$);
\node[anchor=west, inner sep=0, font=\footnotesize] at ($(P)!0.75cm!90:(Q)$){$y=P(x)$};
\end{tikzpicture}
\end{document}