2016-01-21 169 views
1

我有可以用下面的MWE轉載簡單matplotlib令pColor情節:將會產生這種設置正確對齊軸標籤

import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
import numpy as np 

test_data = np.array([[-0.00842278, -0.03332517, -0.01478557, -0.00275494], 
     [ 0.16338327, 0.08383871, 0.03093892, 0.03380778], 
     [-0.02246485, -0.1490697 , -0.14918824, -0.12745594], 
     [ 0.02477743, 0.1537171 , 0.13111042, 0.11950057], 
     [-0.15408288, -0.04697411, -0.0068787 , -0.01576426], 
     [ 0.03508095, 0.19434805, 0.13647802, 0.11276903], 
     [-0.16683297, 0.05313956, 0.0283734 , 0.01179509], 
     [-0.08839198, -0.02095752, -0.00573671, 0.00360559], 
     [ 0.15476156, -0.06324123, -0.04798161, -0.03844384], 
     [-0.056892 , -0.09804484, -0.09506561, -0.08506755], 
     [ 0.2318552 , -0.02209629, -0.04530164, -0.02950514], 
     [-0.11914883, 0.00965362, -0.02431899, -0.0203009 ], 
     [ 0.16025558, 0.02234824, -0.01480751, -0.01487853], 
     [ 0.17345419, -0.04348332, -0.07625766, -0.05771962]]) 

test_df = pd.DataFrame(1 - abs(test_data)) 
test_df.columns = ['3', '6', '9', '12'] 
test_df.index = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '15', '20', '25', '30'] 
plt.pcolor(test_df, cmap=plt.cm.RdYlGn, vmin=0, vmax=1) 
plt.show() 

plot without aligned axis labels

正如可以看到上面的軸標籤不正確,也沒有正確對齊圖的彩色矩形。

我可以在一定程度上創建使用下面的代碼x軸的預期軸標籤:

ax = plt.gca() 
labels = [u'', u'3', u'', u'6', u'', u'9', u'', u'12', u''] 
ax.set_xticklabels(labels) 

將會產生這樣的:

second plot corrected x axis

我的問題是,我不能重現這在y軸上的標籤與矩形的中心不一致。

有沒有一種方法使x和y軸的標籤正確,如數據框標題和索引中所述?同時確保標籤以矩形爲中心,而不是邊緣。

回答

1

我發現另一種解決辦法,我認爲是更簡單,使用sns

import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
%matplotlib # magic command for sns to use matplotlib 

test_data = np.array([[-0.00842278, -0.03332517, -0.01478557, -0.00275494], 
     [ 0.16338327, 0.08383871, 0.03093892, 0.03380778], 
     [-0.02246485, -0.1490697 , -0.14918824, -0.12745594], 
     [ 0.02477743, 0.1537171 , 0.13111042, 0.11950057], 
     [-0.15408288, -0.04697411, -0.0068787 , -0.01576426], 
     [ 0.03508095, 0.19434805, 0.13647802, 0.11276903], 
     [-0.16683297, 0.05313956, 0.0283734 , 0.01179509], 
     [-0.08839198, -0.02095752, -0.00573671, 0.00360559], 
     [ 0.15476156, -0.06324123, -0.04798161, -0.03844384], 
     [-0.056892 , -0.09804484, -0.09506561, -0.08506755], 
     [ 0.2318552 , -0.02209629, -0.04530164, -0.02950514], 
     [-0.11914883, 0.00965362, -0.02431899, -0.0203009 ], 
     [ 0.16025558, 0.02234824, -0.01480751, -0.01487853], 
     [ 0.17345419, -0.04348332, -0.07625766, -0.05771962]]) 


Cols = ['3', '6', '9', '12'] 
Index = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '15', '20', '25', '30'] 
test_df = pd.DataFrame(1 - abs(test_data),index=Index, columns=Cols) 
sns.heatmap(test_df,cmap=plt.cm.RdYlGn,vmin=0,vmax=1,cbar=True) 

正如你會發現,它繪製完全相同的事情,而是直接使用索引和列標籤。

另一個區別是它使用DataFrame的原始索引,因此您的圖形不像Matplotlib解決方案那樣顛倒。

請注意,在我的電腦上,我必須放大窗口以查看顏色條,否則會隱藏。

2

它不是大做這樣的(你是去耦從數據刻度標記),但你可以這樣做:

fig,ax = plt.subplots() 

ax.pcolor(test_df, cmap=plt.cm.RdYlGn, vmin=0, vmax=1) 

ax.set_yticks(np.arange(len(test_df.index))+0.5) 
ax.set_yticklabels(test_df.index) 

ax.set_xticks(np.arange(len(test_df.columns))+0.5) 
ax.set_xticklabels(test_df.columns) 

我們蜱設置爲每0.5, 1.5, 2.5(居中它們)等等,然後設置dataframe索引和列的刻度標籤。

enter image description here

+0

謝謝,擴展我的問題 - 你知道我將如何爲這個例子設置一個彩色條形圖例嗎? – BML91

+0

@ BML91你可以在'plt.show'之前加上'plt.colorbar()'。請注意,colorbar的範圍由'pcolor'中的'vmin'和'vmax' arg定義 – CoMartel