一个很好的特性pgfplots
是它会自动为最大/最小 x 和 y 值以及 x 和 y 单位向量选择合理的值,这尤其会影响网格大小以及 x 和 y 轴上刻度的标签。
另一方面,我知道我可以选择xmin,xmax,...
手动选择值和单位向量。例如,当我希望打印出来的纸上的刻度距离正好是 1 厘米时,我就会这样做。例如,请看我的问题:pgfplots 轴缩放
但是,是否可以合理地将两者结合起来,使得以下情况成立:要求pgfplots
绘制网格线,使得刻度的距离在打印纸上为 1 厘米,但让它自动完成其他所有操作,就像通常那样,使得生成的图表可以很好地适合页面...
如果我的愿望不完全清楚,请询问(我觉得我的英语太差,无法表达清楚……)
编辑
这里给出两个例子,看代码里的注释:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
%In this example everything is pretty nice, except that the grid on the printed paper is
%not exactly 1cm x 1cm
\begin{tikzpicture}
\begin{axis}[grid,no markers,samples=100]
\addplot {exp(x)};
\end{axis}
\end{tikzpicture}
%In this example the grid size on the printed paper is 1cm x 1cm, which I want, but
%the picture is really too large
\begin{tikzpicture}
\begin{axis}[grid,no markers,samples=100,x=1cm,y=1cm]
\addplot {exp(x)};
\end{axis}
\end{tikzpicture}
%Here I adjusted it manually. But I want that it does this automatically. I don't want to think about what will be the maximum value of the function etc.
\begin{tikzpicture}
\begin{axis}[grid=both,no markers,samples=100,x=1cm,y=1cm/50,yticks={0,50,...,200}]
\addplot {exp(x)};
\end{axis}
\end{tikzpicture}
\end{document}
在这种情况下,我不想包含输出,因为在第二个例子中它太大了。
答案1
好的,这是一种方法。您需要为绘图明确指定一个width
和height
,它将被视为绘图的“目标”值。您可能会遇到具有非常大的值或数据范围的绘图问题(如果遇到,请编辑您的问题以包含示例,我会尝试修复它)。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{nicetick}{1}{%
\pgfmathsetmacro\exponent{floor(log10(#1))}%
\pgfmathsetmacro\fraction{#1/(10^\exponent}%
\pgfmathparse{(10 - (\fraction<5)*5 - (\fraction<2)*3 - (\fraction<1)*1)* 10^\exponent
}
}
\makeatletter
\pgfplotsset{
y grid length/.style={
before end axis/.append code={
\pgfplotsset{
calculate/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathparse{\pgfplots@data@ymax-\pgfplots@data@ymin}
\let\datarange=\pgfmathresult
\pgfmathsetmacro\numberofticks{round(\pgfkeysvalueof{/pgfplots/height}/#1)}
\pgfmathsetmacro\niceytick{nicetick( (\datarange)/ (\numberofticks))}
\pgfmathsetmacro\minytick{(floor(\pgfplots@data@ymin/\niceytick)-1) * \niceytick}
\pgfmathsetmacro\secondytick{\minytick+\niceytick}
\pgfmathsetmacro\maxytick{(round(\pgfplots@data@ymax/\niceytick)+1) * \niceytick}
\pgfmathsetmacro\yunitlength{#1/\niceytick}
\pgfkeys{/pgf/fpu/output format=float,/pgf/fpu=false}
},
calculate,
y=\yunitlength pt,
ytick={\minytick,\secondytick,...,\maxytick}
}
}
},
x grid length/.style={
before end axis/.append code={
\pgfplotsset{
calculate/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathparse{\pgfplots@data@xmax-\pgfplots@data@xmin}
\let\dataxrange=\pgfmathresult
\pgfmathsetmacro\numberofticks{round(\pgfkeysvalueof{/pgfplots/width}/#1)}
\pgfmathsetmacro\nicextick{nicetick( (\dataxrange)/ (\numberofticks))}
\pgfmathsetmacro\minxtick{(floor(\pgfplots@data@xmin/\nicextick)-1) * \nicextick}
\pgfmathsetmacro\secondxtick{\minxtick+\nicextick}
\pgfmathsetmacro\maxxtick{(round(\pgfplots@data@xmax/\nicextick)+1) * \nicextick}
\pgfkeys{/pgf/fpu/output format=float,/pgf/fpu=false}
},
calculate,
x=#1/\nicextick,
xtick={\minxtick,\secondxtick,...,\maxxtick}
}
}
}
}
\begin{tikzpicture}
\begin{axis}[
height=4cm,
width=8cm,
grid,
no markers,
samples=100,
title=No fixed grid size
]
\addplot {exp(x)};
\end{axis}
\end{tikzpicture}\\[0.5cm]
\begin{tikzpicture}
\begin{axis}[
height=4cm,
width=8cm,
grid,
no markers,
samples=100,
x grid length=1cm,
y grid length=1cm,
title=Grid size 1\,cm by 1\,cm
]
\addplot {exp(x)};
\end{axis}
\end{tikzpicture}\\[0.5cm]
\begin{tikzpicture}
\begin{axis}[
height=4cm,
width=8cm,
grid,
no markers,
samples=100,
x grid length=2cm,
y grid length=2cm,
title=Grid size 2\,cm by 2\,cm
]
\addplot {exp(x)};
\end{axis}
\end{tikzpicture}
\end{document}