简体   繁体   English

如何让 matplotlib 忘记删除的 plot 元素?

[英]How can I make matplotlib forget about removed plot elements?

I'm using matplotlib within a GUI and elements come and go from the plot. I'm trying to keep the axis tight on the data, but have noticed that if an element is removed, axis('tight') still expands the axis as though it were there.我在 GUI 中使用 matplotlib 并且元素来自 plot 和 go。我试图使轴紧贴数据,但注意到如果删除了一个元素, axis('tight')仍然会扩展轴就好像它在那里一样。 How can this be addressed?如何解决这个问题?

Example:例子:

fig,ax = plt.subplots()
h1 = ax.plot(1,1,'.')
h2 = ax.plot(10,10,'.')
h3 = ax.plot(20,20,'.')
ax.axis('tight')  # (1,20,1,20)
h3[0].remove()
ax.axis('tight')  # still (1,20,1,20), desire (1,10,1,10)

After removing the element from the plot, call ax.relim() on the axis so that it recalculates the limits before calling ax.axis('tight') .从 plot 中删除元素后,调用轴上的ax.relim()以便它在调用ax.axis('tight')之前重新计算限制。

import matplotlib.pyplot as plt

fig,ax = plt.subplots()
h1 = ax.plot(1,1,'.')
h2 = ax.plot(10,10,'.')
h3 = ax.plot(20,20,'.')
ax.axis('tight')  # (1,20,1,20)
h3[0].remove()
ax.relim()        # recalculates that limits for the axis
ax.axis('tight')  # will rescale the plot correctly to the new limits
plt.show()

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

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