Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Axes class - set explicitly size (width/height) of axes in given units

I want to to create a figure using matplotlib where I can explicitly specify the size of the axes, i.e. I want to set the width and height of the axes bbox.

I have looked around all over and I cannot find a solution for this. What I typically find is how to adjust the size of the complete Figure (including ticks and labels), for example using fig, ax = plt.subplots(figsize=(w, h))

This is very important for me as I want to have a 1:1 scale of the axes, i.e. 1 unit in paper is equal to 1 unit in reality. For example, if xrange is 0 to 10 with major tick = 1 and x axis is 10cm, then 1 major tick = 1cm. I will save this figure as pdf to import it to a latex document.

This question brought up a similar topic but the answer does not solve my problem (using plt.gca().set_aspect('equal', adjustable='box') code)

From this other question I see that it is possible to get the axes size, but not how to modify them explicitly.

Any ideas how I can set the axes box size and not just the figure size. The figure size should adapt to the axes size.

Thanks!

For those familiar with pgfplots in latex, it will like to have something similar to the scale only axis option (see here for example).

Answer*

Cancel
8
  • Thank you very much for the help @ImportanceOfBeingErnest !!! this seems to work very well in the generated pdf (like magic!). An important note is that the set_size should be executed just before saving the figure in order to account for all changes in the labels and ticks. However I noticed two problems: (1) if the size of the figure is too small the labels are clipped and (2) when I print the pdf the size does not correspond to the reality scale. Why is that? Note that I call the pdf in a latex document as a figure with \includegraphics and then I print the generated pdf.
    – Gabriel
    Commented Jul 7, 2017 at 15:37
  • (1) That is to be expected. You may want to set larger spacings using fig.subplots_adjust(). (2) by printing you mean print to paper using an ink or laser printer? That would possibly have totally different reasons. First check if the size in the pdf itself is correct. I assume this to be the case, next check if the size in the latex is correct. This may or may not be the case. Finally the printed paper may have a different scale depending on the printer settings. Commented Jul 7, 2017 at 15:42
  • I use the pdf measure tool to check the size of the latex pdf and it is right but not on paper after printing in a laser printer.
    – Gabriel
    Commented Jul 7, 2017 at 15:44
  • I see. But as the latex pdf has the correct size, I think we cannot give support on printing issues on StackOverflow. Check your printer settings, look for similar issues on other pages, possibly on superuser.stackexchange or ask a question there. Commented Jul 7, 2017 at 15:54
  • 2
    You can add an axes for the colorbar (add_axes) and make some space for it using subplots_adjust. Commented Jul 11, 2017 at 15:35