2015-09-14 26 views
6

我正在運行此代碼來構建散佈矩陣。問題在於情節看起來很亂,因爲看不到變量的名字(見下圖)。有什麼方法可以改變標題的方向,並用數字關閉標記?如何自定義散佈矩陣以查看所有標題?

import pandas as pd 
import matplotlib.pyplot as plt 

train = pd.read_csv('data/train.csv', parse_dates=[0]) 

plt.figure() 
a = pd.scatter_matrix(train, alpha=0.05, figsize=(10,10), diagonal='hist') 
plt.show() 

enter image description here

回答

6

作爲最小scatter_matrix例如關掉軸蜱和旋轉標籤,

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 
from pandas.tools.plotting import scatter_matrix 

df = pd.DataFrame(np.random.randn(1000, 4), columns=['long label', 'testing', 'another label', 'something else']) 

sm = scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde') 

#Change label rotation 
[s.xaxis.label.set_rotation(45) for s in sm.reshape(-1)] 
[s.yaxis.label.set_rotation(0) for s in sm.reshape(-1)] 

#May need to offset label when rotating to prevent overlap of figure 
[s.get_yaxis().set_label_coords(-0.3,0.5) for s in sm.reshape(-1)] 

#Hide all ticks 
[s.set_xticks(()) for s in sm.reshape(-1)] 
[s.set_yticks(()) for s in sm.reshape(-1)] 

plt.show() 

並且類似地,可以用任何軸的調整標籤,調整大小等包含在從scatter_matrix返回的句柄中的對象。結果如下:

enter image description here