已关注 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 : Labels vertical (x) and horizontal (y) without being cut-off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58623528/