this之后,我设法根据需要旋转标签。

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


现在的问题是(1)我需要手动调整标签板(这不是一个选择,因为我有很多这样的图是通过循环创建的),并且(2)较长的标签被图的边缘切除了(这可以通过plt.tight_layout()进行部分解决,但这也会增加散点图之间的空间,这是我所不希望的)。我怎样才能解决这个问题?我猜必须有简单的“自动调整”?

最佳答案

与其手动填充标签,不如将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()


python -  Pandas scatter_matrix:标注垂直(x)和水平(y)而不被截断-LMLPHP

关于python - Pandas scatter_matrix:标注垂直(x)和水平(y)而不被截断,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58623528/

10-12 18:17