赞
踩
目录
ax.lines = []
当你尝试使用 ax.lines = []
来清除一个图表的线条,并遇到 AttributeError: can't set attribute
错误时,这表明 lines
属性不能直接被设置或重置,因为它是一个只读属性。
要清除 matplotlib
图表上的所有线条,你需要使用不同的方法。一个常见的做法是通过循环删除每条线。这里是如何做的:
- for coll in ax.lines:
- coll.remove()
- import matplotlib.pyplot as plt
-
- # 假设你已经有一个绘图 ax
- fig, ax = plt.subplots()
- ax.plot([1, 2, 3], [1, 4, 9], label='Line 1')
- ax.plot([1, 2, 3], [2, 5, 8], label='Line 2')
-
- # 清除所有线条
- while len(ax.lines) > 0:
- ax.lines.remove(ax.lines[0])
-
- # 重新显示图表,现在应该没有线条了
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。