2

Consider the following toy example:

import numpy as np
d = np.random.rand(160,100)

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.axis('off')
ax.set_xticklabels([]);
ax.set_yticklabels([]);

ax.imshow(d)

fig.add_axes([0, 0, 0.1, 0.5])

plt.show()

What it looks like

If I remove fig.add_axes then it looks like this:

What it looks like without a second axes

I don't understand the coordinate system used in fig.add_axes. I expected (0,0) to be in the bottom left corner of the second picture, and the coordinates to run from 0 to 1 over the width and height of the second picture. What am I missing?

Please explain how the coordinate system works in this example, and also how I can position the axes relative to the image. The ultimate goal is to position a color bar on top of the image, below it, or to the right.

C. E.
  • 10,297
  • 10
  • 53
  • 77

1 Answers1

2

You seem to be running this in a jupyter notebook. This is an important piece of information. If you try to run the code as a script you will see the expected output. Also when you save the figure as png inside the notebook the output is as expected. You are hence also not doing anything wrong and the interpretation of the coordinates is correct.

The only thing that you need to keep in mind is that pngs inside jupyter are internally saved with the bbox_inches="tight" option, which crops or expands the figure to its content. If only half the figure is filled with an image, the shown figure will be cropped to half the original figure size.

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I did not realize that it mattered, kudos to you for being able to answer anyway! Do you have any tips for using matplotlib inside Jupyter, as in this case for example I wanted to use it for a color bar. Could this be done inside Jupyter? If you don't use Jupyter for generating plots with matplotlib, what do you do instead? Save to file? Thank you. – C. E. Nov 20 '17 at 16:09
  • I think its all fine, you can generate your colorbar in jupyter just as you would within a script. If you tell us the actual problem you have, one might sure assist you in obtaining the desired result. – ImportanceOfBeingErnest Nov 20 '17 at 16:20
  • The problem is that I want to make this axis a certain size and place it where I want to place it, but I don't understand the coordinate system. I understand now why the coordinates are not what I want them to be, why the lower left corner of the image is not at (0,0) for example. But I don't understand what coordinates I should use instead? Like it says in the question, I want to place a color bar either on top of the image, below it, or to the right. What coordinates should I use for this? (The color bar is not in my question, but I intend to add it to the empty axis.) – C. E. Nov 20 '17 at 16:22
  • To be clear: I don't care about how it looks when I use it in a script or how it looks when I save it using code, I want it to look right in the notebook. Thank you for your help. – C. E. Nov 20 '17 at 16:31
  • Well, usually you would not use any specific coordinates to place a simple colorbar. Have you had a look at [this answer](https://stackoverflow.com/a/43425119/4124317)? As said any specific problem may require a specific solution one can help you with. As long as I don't know where "you want to place it" it's hard to help. – ImportanceOfBeingErnest Nov 20 '17 at 16:40
  • It's not about a specific location. The way I understood the documentation was that the coordinates would start at the lower left corner of the figure and then run from 0 to 1 over the figure. Using this information I would have been able to place the axis wherever I want. But the way it seems to work is that when I add the axis the figure changes size. So now I have no idea what the coordinate system means, where is the origin? The coordinates run from 0 to 1 over what area exactly? I want to know how I can place the axis where I want to place it, I don't know how else to ask this. – C. E. Nov 20 '17 at 16:59
  • I think this is a misconception. The coordinates are - as you say - from 0 to 1 in terms of the figure size. So you can place whatever object you like using those coordinates. Think of it as a sheet of paper. You place some image in the middle, then you place some axes for the colorbar somewhere using coordinates relative to the sheet size. Once you finish you take your scissors and cut everything that is left white from the paper. The result will be a smaller sheet of paper, but the coordinates are still valid with respect to your original sheet before cutting it. – ImportanceOfBeingErnest Nov 20 '17 at 17:05
  • My example was meant to show that this is not how it works. That was what I was trying to get at. If what you say is true, then why isn't the axis in my first figure placed so that its bottom left corner aligns with the bottom left corner of the image, takes up half the height of the image and 0.1 of its width? – C. E. Nov 20 '17 at 17:13
  • I added a picture which might help more than explaining it over and over again. – ImportanceOfBeingErnest Nov 20 '17 at 17:25