2016-11-27 180 views
1

無法弄清楚如何簡單地將顏色條添加到矩陣hexbinmatplotlib的地塊。這是地塊的7 7矩陣,這裏的第一行代碼的一部分:Matplotlib hexbin添加顏色條 - 對於傻瓜

#importing nec. libs 
import pandas as pd 
from pylab import rcParams 
from matplotlib import pyplot as PLT 
%matplotlib inline 

#reading data 
dataset_cat_coded_path = 'C:/Users/IBM_ADMIN/Documents/Sberbank/dataset_cat_coded.csv' 
dataset_cat_coded = pd.read_csv(dataset_cat_coded_path, encoding = 'cp1251') 
test_frame = dataset_cat_coded[['opf','priority', 'sales_method_id', 'is_block_vks','sector_id', 'segment_id', 'org_type_id']] 



#building figure 
rcParams['figure.figsize']=30,30 #fig. size 

#fig. initiation 
fig = PLT.figure() 

#setting parameters defining min and max luminance 
vmin = 0 
vmax=test_frame.shape[0] 

#building a row of plots 
ax1 = fig.add_subplot(7,7,2) 
ax1.hexbin(test_frame.opf, test_frame.priority,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax) 
ax1.set_title('priority') 
ax1.set_ylabel('OPF') 

ax2 = fig.add_subplot(7,7,3) 
ax2.hexbin(test_frame.opf, test_frame.sales_method_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax) 
ax2.set_title('sales_method_id') 

ax3 = fig.add_subplot(7,7,4) 
ax3.hexbin(test_frame.opf, test_frame.is_block_vks,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax) 
ax3.set_title('is_block_vks') 

ax4 = fig.add_subplot(7,7,5) 
ax4.hexbin(test_frame.opf, test_frame.sector_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax) 
ax4.set_title('sector_id') 

ax5 = fig.add_subplot(7,7,6) 
ax5.hexbin(test_frame.opf, test_frame.segment_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax) 
ax5.set_title('segment_id') 

ax6 = fig.add_subplot(7,7,7) 
ax6.hexbin(test_frame.opf, test_frame.org_type_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax) 
ax6.set_title('org_type_id') 

#colorbar attempt 
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) 
fig.colorbar(ax6, cax=cbar_ax, location='bottom') here 

錯誤文本:

--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-17-e885eb3cd39a> in <module>() 
    36 
    37 cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) 
---> 38 fig.colorbar(ax6, cax=cbar_ax, location='bottom') 

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw) 
    1591     cax, kw = cbar.make_axes(ax, **kw) 
    1592   cax.hold(True) 
-> 1593   cb = cbar.colorbar_factory(cax, mappable, **kw) 
    1594 
    1595   self.sca(current_ax) 

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\colorbar.py in colorbar_factory(cax, mappable, **kwargs) 
    1328   cb = ColorbarPatch(cax, mappable, **kwargs) 
    1329  else: 
-> 1330   cb = Colorbar(cax, mappable, **kwargs) 
    1331 
    1332  cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed) 

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\colorbar.py in __init__(self, ax, mappable, **kw) 
    878   # Ensure the given mappable's norm has appropriate vmin and vmax set 
    879   # even if mappable.draw has not yet been called. 
--> 880   mappable.autoscale_None() 
    881 
    882   self.mappable = mappable 

AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None' 

所得的情節: enter image description here

+0

有很多例子,所以你到目前爲止嘗試過什麼?另外,你需要告訴你是否要爲整個圖形添加一個顏色條?如果是這樣,所有數據的數據限制是否相同?或者你想每個圖有一個彩條? – ImportanceOfBeingErnest

+0

數據限制是相同的,所以整個數字的1個色條。說實話,如果我能夠在數字之前做到這一點,那也是可以的。問題是我無法理解在我的特殊情況下通過plt.colorbar()使其工作的參數。 –

+0

@ImportanceOfBeingErnest 溶液形成你已經引用的頁面 'cbar_ax = fig.add_axes([0.85,0.15,0.05,0.7]) fig.colorbar(1M,CAX = cbar_ax)' 給出錯誤*'AxesSubplot 'object has no attribute'autoscale_None'* –

回答

0

正如我指出的評論this question顯示瞭如何添加一個顏色條。

在你的代碼

fig.colorbar(ax6, cax=cbar_ax, location='bottom') 

您嘗試設置一個彩條的數字但沒有指定哪個對象應該涉及。因此,你會得到一個錯誤,這基本上告訴你,你沒有提供任何可映射的對象。

你需要做的是給你的hexbin圖像的顏色,以便它知道實際繪製哪些顏色。

import numpy as np 
import matplotlib.pyplot as plt 

n = 1000 
x = np.random.standard_normal(n) 
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n) 

fig, axes = plt.subplots(nrows=2, ncols=2) 
for ax in axes.flat: 
    im = ax.hexbin(x,y) 

fig.subplots_adjust(right=0.8) 
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) 
fig.colorbar(im, cax=cbar_ax) 

plt.show() 
+0

是的,非常感謝您提供最終答案! 現在我必須弄清楚如何改變它的大小,並把它帶到整個矩陣的底部。 –

+0

這裏的數字'fig.add_axes([0.85,0.15,0.05,0.7])'確定了顏色條的位置和大小。第一個數字定義了條的最左側應該位於整個圖的水平軸上的位置,左下角爲零。 第二個數字在垂直軸上確定相同。如果數字爲[0.1,0.1],則條從左下角延伸。第三個數字以圖的部分(= 0.5 - >色條延伸一半的數字)來確定色條的長度。最後一個數字以相同的條件確定寬度。 –

+0

確實,['add_axes(rect)'](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.add_axes)以矩形作爲輸入,其中'rect = [left,bottom,width ,高度]'。這些數字以圖形座標系統爲單位(範圍從左下角的「0,0」到右上角的「1,1」)。 – ImportanceOfBeingErnest