我有一张带有图像的图,它是用下面的代码创建的,并渲染为下图中的顶部图。如果我确定部分图包含不必要的数据,我该如何裁剪掉它,使其像下面的第二张图一样渲染?此外,图像的长宽比不扭曲也很重要。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={x axis},ylabel={y axis},
xtick distance=1,ytick distance=1,
width=10cm,height=4.44062cm,
enlargelimits=false,
scale only axis=true,
tick align=outside,]
\addplot graphics[xmin=0, xmax=5, ymin=0, ymax=5] {Images/test};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您可以剪辑图像,但恕我直言,您必须手动计算高度键的变化。此外,最好保留图像的比例(这会使计算更容易):
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\newsavebox\mygraphic
\savebox\mygraphic{\includegraphics{plot}}
\begin{tikzpicture}
\begin{axis}[
xlabel={x axis},ylabel={y axis},
xtick distance=1,ytick distance=1,
width=0.5\wd\mygraphic,height=0.5\ht\mygraphic,
enlargelimits=false,
scale only axis=true,
tick align=outside,]
\addplot graphics[xmin=0, xmax=5, ymin=0, ymax=5,includegraphics={keepaspectratio}] {plot};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
xlabel={x axis},ylabel={y axis},
xtick distance=1,ytick distance=1,
width=0.5\wd\mygraphic,height=0.8333\ht\mygraphic, %height= 5/3*0.5
enlargelimits=false,
scale only axis=true,
tick align=outside,]
\addplot graphics[xmin=1, xmax=4, ymin=0, ymax=5, includegraphics={trim=0.2\wd\mygraphic{} 0pt 0.2\wd\mygraphic{} 0pt,clip,keepaspectratio}] {plot};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
就像是
\addplot graphics[xmin=0.8, xmax=3.8, ymin=0.95, ymax=5,
includegraphics={trim=50 72 110 0,clip,keepaspectratio}] {Images/test};
修剪坐标给出了图片四边的修剪量,以 Postscript 点为单位。因此,您必须先测量图像。然后,您需要稍微调整一下才能正确显示。我认为在图像编辑器中裁剪图像更容易。这样,纵横keepaspectratio
比就不会改变。如果您的修剪与纵横比不一致,则在顶部或右侧将显示为空白。然后您可以调整它。