Python matplotlib:有没有办法获得现有的颜色条?
在matplotlib的面向对象样式中,可以获得现有地物中的当前轴、线和图像:Python matplotlib:有没有办法获得现有的颜色条?,python,matplotlib,Python,Matplotlib,在matplotlib的面向对象样式中,可以获得现有地物中的当前轴、线和图像: fig.axes fig.axes[0].lines fig.axes[0].images 但我还没有找到获取现有颜色栏的方法,我必须在首次创建颜色栏时为其指定一个名称: cbar = fig.colorbar(image) 如果我没有指定颜色条对象的名称,有没有办法获得给定图形中的颜色条对象?问题是颜色条被添加为“只是另一个”轴,因此它将与“正常”轴一起列出 import matplotlib.pyplot a
fig.axes
fig.axes[0].lines
fig.axes[0].images
但我还没有找到获取现有颜色栏的方法,我必须在首次创建颜色栏时为其指定一个名称:
cbar = fig.colorbar(image)
如果我没有指定颜色条对象的名称,有没有办法获得给定图形中的颜色条对象?问题是颜色条被添加为“只是另一个”轴,因此它将与“正常”轴一起列出
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(6,6)
fig = plt.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
cax = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
print "Before adding the colorbar:"
print fig.axes
fig.colorbar(cax)
print "After adding the colorbar:"
print fig.axes
对我来说,这给出了以下结果:
添加颜色栏之前:
[]
添加颜色栏后:
[,
]
也就是说,图形中有两个轴,第二个轴是新的颜色条
编辑:代码基于此处给出的答案:
Before adding the colorbar:
[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000080D1D68>]
After adding the colorbar:
[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000080D1D68>,
<matplotl ib.axes._subplots.AxesSubplot object at 0x0000000008268390>]