0

我希望因子图(即日期)上的 X 轴标签在屏幕上垂直而不是水平(因为水平占用太多空间)。

在此处输入图像描述

4

1 回答 1

0

这是,IMO,matplotlib 最棘手的一点。您必须单独旋转每个标签。

我总是把这个功能放在手边:

def rotateTickLabels(ax, rotation, which, rotation_mode='anchor', ha='right'):
    axes = []
    if which in ['x', 'both']:
        axes.append(ax.xaxis)

    elif which in ['y', 'both']:
        axes.append(ax.yaxis)

    for axis in axes:
        for t in axis.get_ticklabels():
            t.set_horizontalalignment(ha)
            t.set_rotation(rotation)
            t.set_rotation_mode(rotation_mode)

那么你可以像这样使用它:

import matploltib.pyplot as plt
fig, ax = plt.subplots()
# ... plot stuff
rotateTickLabels(ax, 30, 'x')
于 2014-12-08T16:40:26.507 回答