简体   繁体   English

pyplot图的add_axes将新轴附加到具有相同位置的旧轴

[英]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. 当我尝试在原先放置另一个轴的同一位置上使用fig.add_axes将轴添加到pyplot图形中时,pyplot只是引用了旧轴。

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. 我尝试将ax.set_position的“哪个”参数设置为“两者”,“活动”和“原始”,但是没有一个参数。

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? 我如何获得它,例如ax2是未引用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. 该问题将在下一版的matplotlib中修复,但与此同时,可以通过为每个轴指定标签来绕过此问题。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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