我已经使用一个复杂的程序创建了一个图,并且想要将该图的内容放入中pgfplots' axis environment
。自然,\addplot graphics
是实现此目的的方法。图像的纵横比(在本例中)为height > width
。但是,两个轴具有不同的缩放比例x/cm >> y/cm
。现在,如果没有axis equal image
,pgfplots
则往往会创建方形图。打开axis equal image
,将使用创建图x/cm = y/cm
。这两个选项都会导致绘图不遵循原始图像的纵横比。到目前为止,我发现的唯一保持纵横比的方法是手动定义和x
。y
unit vectors
但这可能很难做到,具体取决于原始图像的大小和轴的不同缩放比例。
有没有一种简单的(最好是自动的)方法来创建一个图像图,无论轴的缩放比例如何,都能保持图像的纵横比?
下面的代码说明了这个问题。
\documentclass[10pt,varwidth=300pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{mwe}
\begin{document}
\includegraphics[width=100pt]{example-image-10x16}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=10,ymin=0,ymax=1,
enlargelimits=false,
axis equal image,
x=1cm,y=16cm,%In this case, x and y are easy to calculate, but this will not always be the case
]
\addplot graphics [xmin=0,xmax=10,ymin=0,ymax=1] {example-image-10x16};
\end{axis}
\node[red,anchor=south] at (current axis.north) {This is what I want};
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=10,ymin=0,ymax=1,
enlargelimits=false,
axis equal image,
]
\addplot graphics [xmin=0,xmax=10,ymin=0,ymax=1] {example-image-10x16};
\end{axis}
\node[red,anchor=south,align=center] at (current axis.north) {This is what I get without \\ manually defining unit vectors};
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=10,ymin=0,ymax=1,
enlargelimits=false,
% axis equal image,
]
\addplot graphics [xmin=0,xmax=10,ymin=0,ymax=1] {example-image-10x16};
\end{axis}
\node[red,anchor=south,align=center] at (current axis.north) {This is what I get without \\ manually defining unit vectors and \\ without axis equal image};
\end{tikzpicture}
\end{document}
答案1
这是 的功能请求pgfplots
。 的当前实现\addplot graphics
可以将图形挤压到轴中。但根据图形的自然大小重新配置轴仅适用于高级\addplot3 graphics[points=...]
。
有趣的是,您的示例中说明的用例实际上比其他用例更简单……一定是被遗忘了。
以下是支持该宏的草稿:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\makeatletter
\newcommand\addplotgraphicsnatural[2][]{%
\begingroup
% set options in this local group (will be lost afterwards):
\pgfqkeys{/pgfplots/plot graphics}{#1}%
% measure the natural size of the graphics:
\setbox0=\hbox{\includegraphics{#2}}%
%
% compute the required unit vector ratio:
\pgfmathparse{\wd0/(\pgfkeysvalueof{/pgfplots/plot graphics/xmax} - \pgfkeysvalueof{/pgfplots/plot graphics/xmin})}%
\let\xunit=\pgfmathresult
\pgfmathparse{\ht0/(\pgfkeysvalueof{/pgfplots/plot graphics/ymax} - \pgfkeysvalueof{/pgfplots/plot graphics/ymin})}%
\let\yunit=\pgfmathresult
%
% configure pgfplots to use it.
% The \xdef expands all macros except those prefixed by '\noexpand'
% and assigns the result to a global macro named '\marshal'.
\xdef\marshal{%
\noexpand\pgfplotsset{unit vector ratio={\xunit\space \yunit}}%
}%
\endgroup
%
% use our macro here:
\marshal
%
\addplot graphics[#1] {#2};
}
\makeatother
\begin{tikzpicture}
\begin{axis}[
axis on top,
xmin=0,xmax=10,ymin=0,ymax=1,
enlargelimits=false,
]
\addplotgraphicsnatural[xmin=0,xmax=10,ymin=0,ymax=1]{example-image-10x16}
\end{axis}
\node[red,anchor=south] at (current axis.north) {This is what I want};
\end{tikzpicture}
\end{document}
新的宏从选项中读取(强制)限制并据此计算正确的单位向量比。
我的形象example-image-10x16
是
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepgfplotslibrary{patchplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90},axis equal image,hide axis]
\addplot3[surf,shader=interp,patch type=bilinear,
mesh/color input=explicit]
coordinates {
(0,0,0) [color=blue] (10,0,0) [color=green]
(0,16,0) [color=yellow] (10,16,1) [color=red]
};
\end{axis}
\end{tikzpicture}
\end{document}