python - Pandas scatter_matrix : Labels vertical (x) and horizontal (y) without being cut-off

标签 python pandas plot

已关注 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()

enter image description here

关于python - Pandas scatter_matrix : Labels vertical (x) and horizontal (y) without being cut-off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58623528/

相关文章:

python - MatPlotLib:给定数据集 3,绘制 2 的图表并使用 3 作为标签

r - 在 R 中的 svyplot() 中用颜色填充气泡(调查包)

javascript - Webstorm 不刷新修改后的 JavaScript 文件

python - Python 中的 OpenCV。获取网络摄像头流有效,但会打印错误消息

python - 银行交易分类的 Tensorflow 实现

python - 如何将 for 循环的结果存储在数据框中并导出到 Excel?

r - 使用来自不同变量的数据在条形图中绘制百分比

Python - Selenium 通过 href 定位元素

python - Pandas Pivot_Table 定义函数 aggfunc

python - str.contains 的可扩展解决方案与 pandas 中的字符串列表