1

The objective is to insert a sub_figure in a simple plot as follows:

import numpy as np
from matplotlib import pyplot as plt
X = np.linspace(-6, 6, 1024)
Y = np.sinc(X)
X_detail = np.linspace(-3, 3, 1024)
Y_detail = np.sinc(X_detail)
plt.plot(X, Y, c = 'k')
sub_axes = plt.axes([0.6,0.6,0.25,0.25])
sub_axes.plot(X_detail, Y_detail, c = 'k')
plt.setp(sub_axes)
plt.show() 

The code above gives the following output:

enter image description here

The matplotlib documentation says the argument the matplotlib.pyplot.axes() function takes is a list defined as rect=[left, bottom, width, height] where the coordinates left, bottom, width, height are added as normalized (0,1) values. Can anyone explain that to me ?

The last two co-ordinates are for the size of the sub_figure, that much I get, now what is the deal with the first two ?

10
  • 1
    The coordinates of the bottom left corner? Commented Jul 31, 2017 at 14:42
  • But then the coordinates of the bottom left corner must be (0.6,0.6) ? @DavidG Commented Jul 31, 2017 at 14:45
  • What's the point of plt.setp(sub_axes)? Commented Jul 31, 2017 at 14:45
  • 2
    The coordinates are in figure space, not data space. The left edge of the figure is x=0, the right edge is x=1. Same for bottom and top, respectively. So your sub-axis will be just to the right and just above the middle of the figure, regardless of how the data is plotted. Commented Jul 31, 2017 at 14:46
  • 1
    Is this question solved or would you like to specify the coordinates in a different coordinate system than figure coordinates? In case you are happy with the answer below, don't forget to accept it. Commented Jul 31, 2017 at 17:15

1 Answer 1

3

The confusion appears to be coming from the different coordinate systems that matplotlib uses. Here is a link to the (fairly exhaustive) tutorial on the subject: https://matplotlib.org/users/transforms_tutorial.html. I will summarize the key point that affect you directly here.

The coordinates you see on your axes are called the data space or data coordinates. This is basically the xlim and ylim of the plots. Note that these are totally independent for the two plots and are not affected by the size or position of your figure.

When you say sub_axes = plt.axes([0.6,0.6,0.25,0.25]), you are specifying the coordinates in figure space or figure coordinates. This is very similar conceptually to axis space or axis coordinates, except that it applies to the whole figure rather than just an individual set of axes.

In this case, the origin of your sub-axes is at (0.6, 0.6) relative to the bottom left corner of the figure. Where the upper-right corner of the figure is (1, 1). As expected, the sub-axes start just a bit above and to the right of the middle of the figure window.

Similarly, the width is (0.25, 0.25), meaning that the sub-axes are 1/4 the size of your figure in each dimension. This can also be interpreted to mean that the upper right-hand corner of the sub-axes is at (0.85, 0.85) in figure space, which looks about right.

You can do some tests. No matter how you pan or zoom on the main axes, the sub-axes are not affected. However, if you resize your figure, both sets of axes will change size to compensate. The sub-axes should always have the same aspect ratio as the figure itself because of how you sized them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.