Pandas scatter_matrix:标记垂直(x)和水平(y)而不被截断

lor*_*rdy 7 python plot pandas

之后,我成功地按照我想要的方式旋转标签。

axes = pd.plotting.scatter_matrix(df_plot, alpha=0.1, figsize=[10, 10])
n = len(df_plot.columns) - 1
    for x in range(n):
        for y in range(n):
            # to get the axis of subplots
            ax = axes[x, y]
            # to make x axis name vertical
            ax.xaxis.label.set_rotation(90)
            # to make y axis name horizontal
            ax.yaxis.label.set_rotation(0)
            # to make sure y axis names are outside the plot area
            ax.yaxis.labelpad = 25
Run Code Online (Sandbox Code Playgroud)

现在的问题是(1)我需要手动调整标签板(这不是一个选项,因为我有许多从循环创建的此类图)和(2)较长的标签被图形边缘截断(这可以通过 plt.tight_layout() 部分解决,但这也增加了我不想要的散点图之间的空间)。我怎样才能解决这个问题?我想一定有简单的“自动调整”?

Ste*_*tef 17

无需手动填充标签,只需右对齐 y 标签文本即可。然后使用tight_layout()防止标签被切断,最后重新将子图之间的间距调整为零。

import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame(pd.np.random.randn(1000, 4), columns=['A long column name','B is also long','C is even longer ','D is short'])
axes = pd.plotting.scatter_matrix(df, alpha=0.2)
for ax in axes.flatten():
    ax.xaxis.label.set_rotation(90)
    ax.yaxis.label.set_rotation(0)
    ax.yaxis.label.set_ha('right')

plt.tight_layout()
plt.gcf().subplots_adjust(wspace=0, hspace=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述