简体   繁体   中英

add_axes to pyplot figure attaches new axis to old axis with same position

When I try to add an axis to a pyplot figure using fig.add_axes on the same location where another axis originally was placed, pyplot simply make a reference to the old axis.

I have a request where a figure is made and afterwards axis can be added. Depending on the request, different axis are added.

I have tried to se the "which" parameter of ax.set_position to "both", "active" and "original", but none does it.

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_axes([0.07, 0.1, 0.88, 0.2])
ax1.set_position([0.07, 0.3, 0.88, 0.2], which='active')
ax2 = fig.add_axes([0.07, 0.1, 0.88, 0.2])
ax2.set_position([0.07, 0.3, 0.88, 0.5])

# When trying to set position of either figure it simply moves them both
# as ax2 is a reference to ax1
ax1.set_position([0.07, 0.1, 0.88, 0.2])

How can I get it, such that ax2 is an independent object not referenced to ax1?

I found an answer myself - here it is, in case any one else face a similar issue.

The problem will be fixed in the next version of matplotlib, but in the mean time, one can bypass it by specifying a label to each axis.

import matplotlib.pyplot as plt
fig = plt.figure()

ax1 = fig.add_axes([0.07, 0.1, 0.88, 0.2], label='no_1')
ax1.set_position([0.07, 0.3, 0.88, 0.2], which='both')
ax2 = fig.add_axes([0.07, 0.1, 0.88, 0.2], projection=None)
ax2.set_position([0.07, 0.1, 0.88, 0.5], label='no_2')

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