为了发表一篇文章,我正在用 PGFPlots 准备多个图,并希望它们看起来都一样,即具有相同的轴和相同的刻度。为此,我打算使用ymin
和ymax
,但 PGFPlots 似乎不尊重scaled ticks=true
。
我试图将其归结为下面这个相当简单的例子,展示设置不同参数的效果。我想实现右下角的图,但是和缩放刻度。我做错了什么?
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,width=6cm}
\usepackage[pdftex,active,tightpage]{preview}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}[ title=Nothing ]
\addplot [red] coordinates { (0,0.0003) (1,0.0004) };
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ title={Scaled Ticks \emph{false}},
scaled ticks=false ]
\addplot [green] coordinates { (0,0.0003) (1,0.0004) };
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ title={Scaled ticks \emph{true} with ymax},
scaled ticks=true,
ymax=0.0005 ]
\addplot [blue] coordinates { (0,0.0003) (1,0.0004) };
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ title={Scaled ticks \emph{true} with ymax \& ymin},
scaled ticks=true,
ymax=0.0005,
ymin=0 ]
\addplot [cyan] coordinates { (0,0.0003) (1,0.0004) };
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
答案1
这似乎是 PGFPlots 中的一个错误(更新2012 年 1 月 22 日:这现已修复): 每当轴的最小值或最大值正好为 0 时,就不会应用缩放。您可以修补内部宏来检查并解决此问题。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,width=6cm}
%
\makeatletter
\def\pgfplots@init@scaled@tick@for#1{%
\global\def\pgfplots@glob@TMPa{0}%
\expandafter\pgfplotslistcheckempty\csname pgfplots@prepared@tick@positions@major@#1\endcsname
\ifpgfplotslistempty
% we have no tick labels. Omit the tick scale label as well!
\else
\begingroup
\ifcase\csname pgfplots@scaled@ticks@#1@choice\endcsname\relax
% CASE 0 : scaled #1 ticks=false: do nothing here.
\or
% CASE 1 : scaled #1 ticks=true:
%--------------------------------
% the \pgfplots@xmin@unscaled@as@float is set just before the data
% scale transformation is initialised.
%
% The variables are empty if there is no datascale transformation.
\expandafter\let\expandafter\pgfplots@cur@min@unscaled\csname pgfplots@#1min@unscaled@as@float\endcsname
\expandafter\let\expandafter\pgfplots@cur@max@unscaled\csname pgfplots@#1max@unscaled@as@float\endcsname
%
\ifx\pgfplots@cur@min@unscaled\pgfutil@empty
\edef\pgfplots@loc@TMPa{\csname pgfplots@#1min\endcsname}%
\expandafter\pgfmathfloatparsenumber\expandafter{\pgfplots@loc@TMPa}%
\let\pgfplots@cur@min@unscaled=\pgfmathresult
\edef\pgfplots@loc@TMPa{\csname pgfplots@#1max\endcsname}%
\expandafter\pgfmathfloatparsenumber\expandafter{\pgfplots@loc@TMPa}%
\let\pgfplots@cur@max@unscaled=\pgfmathresult
\fi
%
\expandafter\pgfmathfloat@decompose@E\pgfplots@cur@min@unscaled\relax\pgfmathfloat@a@E
\expandafter\pgfmathfloat@decompose@E\pgfplots@cur@max@unscaled\relax\pgfmathfloat@b@E
\ifnum\pgfmathfloat@a@E=0%
\pgfmathfloat@a@E=\pgfmathfloat@b@E%
\fi%
\ifnum\pgfmathfloat@b@E=0%
\pgfmathfloat@b@E=\pgfmathfloat@a@E%
\fi%
\ifnum\pgfmathfloat@b@E<\pgfmathfloat@a@E%
\pgfmathfloat@b@E=\pgfmathfloat@a@E%
\fi
\xdef\pgfplots@glob@TMPa{\pgfplots@scale@ticks@above@exponent}%
\expandafter\ifnum\pgfplots@glob@TMPa<\pgfmathfloat@b@E
% ok, scale it:
\multiply\pgfmathfloat@b@E by-1
\xdef\pgfplots@glob@TMPa{\the\pgfmathfloat@b@E}%
\else
\xdef\pgfplots@glob@TMPa{\pgfplots@scale@ticks@below@exponent}%
\expandafter\ifnum\pgfplots@glob@TMPa>\pgfmathfloat@b@E
% ok, scale it:
\multiply\pgfmathfloat@b@E by-1
\xdef\pgfplots@glob@TMPa{\the\pgfmathfloat@b@E}%
\else
% no scaling necessary:
\xdef\pgfplots@glob@TMPa{0}%
\fi
\fi
\or
% CASE 2 : scaled #1 ticks=base 10:
%--------------------------------
\c@pgf@counta=\csname pgfplots@scaled@ticks@#1@arg\endcsname\relax
%\multiply\c@pgf@counta by-1
\xdef\pgfplots@glob@TMPa{\the\c@pgf@counta}%
\or
% CASE 3 : scaled #1 ticks=real:
%--------------------------------
\pgfmathfloatparsenumber{\csname pgfplots@scaled@ticks@#1@arg\endcsname}%
\global\let\pgfplots@glob@TMPa=\pgfmathresult
\or
% CASE 4 : scaled #1 ticks=manual:
\expandafter\global\expandafter\let\expandafter\pgfplots@glob@TMPa\csname pgfplots@scaled@ticks@#1@arg\endcsname
\fi
\endgroup
\fi
\expandafter\let\csname pgfplots@tick@scale@#1\endcsname=\pgfplots@glob@TMPa%
}
\makeatother
%
\begin{document}
\begin{tikzpicture}
\begin{axis}[ title={Scaled ticks \emph{true} with ymax \& ymin},
scaled y ticks=true,
ymax=0.0008, ymin=0
]
\addplot [cyan, domain=0:10] {0.0001*x};
\end{axis}
\end{tikzpicture}
\end{document}