7

Following this I have managed to rotate the labels as I want them.

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

The problem is now that (1) I need to adjust the labelpad manually (which is not an option as I have many such plot created from a loop) and (2) that longer labels are cut-off by the edge of the figure (this can partly be addressed with plt.tight_layout() but this also increases the space between the scatter plot which I don't want). How can I fix this? I guess there must be simple "auto adjust"?

2
  • Have you tried plt.tight_layout()?
    – powerPixie
    Commented Oct 30, 2019 at 10:47
  • @powerPixie, yes but as said above it increases the spaces between the scatter plots and also does not fix the padding
    – lordy
    Commented Oct 30, 2019 at 10:49

1 Answer 1

18

Instead of manually padding the labels just right align the y label text. Then use tight_layout() to prevent labels from being cut off and finally re-adjust the spacing between the subplots to zero.

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()

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.