我正在运行此代码来构建分散矩阵。问题是情节看起来很乱,因为不可能看到变量的名称(见下图)。有没有办法改变标题的方向,并用数字关闭标记?
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()
答案 0 :(得分: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
返回的句柄中包含的任何轴对象调整标签,调整大小等。这导致,
答案 1 :(得分:1)
pandas.tools.plotting.scatter_matrix
现在已弃用。改用 pandas.plotting.scatter_matrix
。
从 Ed Smith (@ed-smith) 提出的更新代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(np.random.randn(1000, 4), columns=['long label', 'testing', 'another label', 'something else'])
sm = pd.plotting.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()