3

I'm trying to insert an image with added axes using pgfplots, which has the same aspect ratio as the original image, i.e. if the x axis is half the size of the width of the image, then the y axis needs to also be half the size of the height of the image (as opposed to the pgfplots default, which seems to distort everything into an approximate square shape).

The simplest way to achieve this seems to be to just calculate what the width and height values should be, then manually define them in the code. The problem is this isn't working. The resulting plot isn't quite the same dimensions as what I specified (height seems a little smaller than it should be) and the image is noticeably distorted. What is going on, and how can I fix this?

Below is the code I've been using:

\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]
    \addplot graphics[xmin=0, xmax=5, ymin=0, ymax=5] {Images/test};
    \end{axis}
\end{tikzpicture}
\end{document}

Test image used in above code: (581px wide, 258px high) test image

Bonus: is there a way to get images in plots to automatically retain their aspect ratio when resized? I had a recent question on this topic that didn't get any responses. This would be ideal, but manually scaling it as above is ok too.

  • 3
    scale only axis? – Torbjørn T. Nov 11 '16 at 4:13
  • 3
    I'm voting to close this question because it was solved in the comments. – Stefan Pinnow Nov 11 '16 at 9:26
  • 1
    @clemens, that is an option. But who should write it? Of course this should be Torbjørn, because he wrote the answer and so deserves the credit. But as far as I "know" him, he won't do it and so I think it is better to close the question instead of letting it unanswered. But feel free to write an answer, if you like. – Stefan Pinnow Nov 11 '16 at 12:21
  • 1
    @StefanPinnow Why do you say that, I mostly do write answers if asked. If I don't write an answer in the first place or when asked, it's because a) I'm not sure it is a solution to the problem or b) I'm fairly sure a duplicate can be found. In this case I expected that a duplicate could be found, as I think the confusion on what width/height for an axis means has popped up before, though a couple minutes searching didn't lead to any good candidates. In general though, I'd prefer that one asked for an answer first, or wrote a CW answer if a very clear solution is found, (ctd.) – Torbjørn T. Nov 11 '16 at 12:55
  • 1
    and closed as OT last. Personally I don't think the very quick closing-as-OT-because-solved-in-comments is the way to go, though for old abandoned questions where it may not even be clear what fixed it, it certainly can be appropriate. (But that discussion is OT here, you can always ping me in chat.) (Edit: the best potential dupe I did find was this, which while different has the same underlying issue.) – Torbjørn T. Nov 11 '16 at 12:58
3

Thanks to @TorbjørnT. for pointing out the issue. This is just a more detailed summary of their solution.

This issue comes about due to a misunderstanding of the meaning of width and height. These refer to the dimensions of the bounding box of the final figure, i.e. the area containing the plot, axes, titles, a small amount of surrounding white space, etc. It was incorrectly assumed these referred to the dimensions of the axes, however they can be redefined to have this meaning by including the line scale only axis=true in the axis environment. Thus the final code will be as follows:

\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]
    \addplot graphics[xmin=0, xmax=5, ymin=0, ymax=5] {Images/test};
    \end{axis}
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.