2014-12-05 35 views
3

在matplotlib的面向對象的風格,你可以得到在現有的圖中的當前座標,線條和圖像:matplotlib:任何獲取現有顏色條的方法?

fig.axes 
fig.axes[0].lines 
fig.axes[0].images 

但我還沒有找到一個辦法讓現有的colorbars,我必須指定彩條一創建它的時候名字:

cbar = fig.colorbar(image) 

有沒有什麼辦法讓顏色條物體在給定的數字,如果我沒有爲它們分配名字?

回答

1

問題是,顏色條被添加爲「另一個」軸,因此它將與「正常」軸一起列出。

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>] 

也就是說,有你的身影兩軸,第二個是新的彩條。

編輯:代碼是基於給出的答案在這裏: https://stackoverflow.com/a/2644255/2073632