简体   繁体   中英

Move axes relative to figure in matplotlib

I am trying to leave some whitespace on the left side of my figure in matplotlib and cannot figure out how to do this.

From the docs, I understand that using the add_axes() method on a figure, I can place an axes at an arbitrary location.

For example, the code below should create the axes on the right half of the figure:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6,4))
fig.add_axes([0.5, 0, 0.5, 1])

However, if you run this, the axes will appear on the left half of the figure instead. Is there something I am missing here?

You can use the add_axes method, to place the axes on an arbitrary position. The position list must contain the positions of the origin (measured from bottom left), and the height and width as follows:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6,4))
# [x0, y0, width, height]
fig.add_axes([0.3, 0.1, 0.6, 0.8])

All relative in the figure (values between zero and 1)


However, if you want to use the default margins, and maintain the position of the other axes, you can first obtaining the original position, update it, and then set it again as follows.

fig = plt.figure(figsize=(6,4))
ax = plt.axes()

pos = ax.get_position()
pos.x0 = 0.2       # for example 0.2, choose your value
ax.set_position(pos)

pos contains x0 , x1 , y0 and y1 , which are the positions of the Bbox of the axes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM