在我使用下面的脚本创建一些axis3D和bar3D后,我想删除/删除某些栏。
似乎我不能使用remove()/ del函数/命令,因为bar3D不支持它?
如果我使用delaxes(),则会删除整个轴。
使bar3D透明对我来说也是可以接受的。但是我不能设置“alpha”字母'已创建的bar3D的值。
它经常显示" TypeError:' NoneType'对象不可迭代"
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=plt.figaspect(0.33))
ax1 = fig.add_subplot(1,3,1, projection='3d')
ax1.set_xlim3d(0, 14)
ax1.set_ylim3d(0, 18)
ax1.set_zlim3d(0, 30)
ax2 = fig.add_subplot(1,3,2,projection='3d')
ax2.set_xlim3d(0, 20)
ax2.set_ylim3d(0, 16)
ax2.set_zlim3d(0, 18)
ax3=fig.add_subplot(1,3,3,projection='3d')
ax3.set_xlim3d(0, 15)
ax3.set_ylim3d(0, 17)
ax3.set_zlim3d(0, 10)
ax1.bar3d(0,0,0,8,5,8, color='b', zsort='average')
ax2.bar3d(0,0,0,6,14,6, color='r', zsort='average')
ax2.bar3d(8,0,0,9,10,3, color='g', zsort='average')
ax3.bar3d(0,0,0,9,10,3, color='y', zsort='average')
ax3.bar3d(0,0,6,7,7,3, color='coral', zsort='average')
""" # I want to delete just the bar3d in ax3! None of them works.
bar3d.remove()
ax3.bar3d.remove()
del ax3.bar3d[0]
fig.delaxes(ax3)
"""
plt.show()
答案 0 :(得分:0)
bar3d
创建Poly3DCollection
个对象。对于每个Axes
对象,它们存储在名为collections
:
In [31]: ax3.collections
Out[31]:
[<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x10bd67bd0>,
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x109c822d0>]
因此,要删除它们,您可以使用任何列表操作,例如pop()
:
ax3.collections.pop()
这将删除您创建的最后一个Poly3DCollection