13

我用来autofmt_xdate以可读的方式绘制长 x 轴标签。问题是,当我想组合不同的子图时,其他子图的 x 轴标签消失了,对于下图中最左边的子图(两行高),我不欣赏。有没有办法防止autofmt_xdate淬火其他 x 轴标签?还是有另一种方法来旋转标签?如您所见,我也尝试过xticks并“旋转”,但结果并不令人满意,因为标签围绕其中心旋转,导致标签混乱。

产生以下情节的脚本:

from matplotlib import pyplot as plt
from numpy import arange
import numpy
from matplotlib import rc

rc("figure",figsize=(15,10))
#rc('figure.subplot',bottom=0.1,hspace=0.1)
rc("legend",fontsize=16)
fig = plt.figure()


Test_Data = numpy.random.normal(size=20)

fig = plt.figure()
Dimension = (2,3)
plt.subplot2grid(Dimension, (0,0),rowspan=2)
plt.plot(Test_Data)
plt.subplot2grid(Dimension, (0,1),colspan=2)
for i,j in zip(Test_Data,arange(len(Test_Data))):
    plt.bar(i,j)
plt.legend(arange(len(Test_Data)))
plt.subplot2grid(Dimension, (1,1),colspan=2)
xticks = [r"%s (%i)" % (a,b) for a,b in zip(Test_Data,Test_Data)]
plt.xticks(arange(len(Test_Data)),xticks)
fig.autofmt_xdate()
plt.ylabel(r'$Some Latex Formula/Divided by some Latex Formula$',fontsize=14)
plt.plot(Test_Data)
#plt.setp(plt.xticks()[1],rotation=30)
plt.tight_layout()
#plt.show()

脚本创建的图

4

1 回答 1

15

这实际上是该autofmt_xdate方法的一个特点。从autofmt_xdate方法的文档中:

日期刻度标签经常重叠,因此旋转它们并右对齐很有用。此外,一个常见的用例是许多具有共享 x 轴的子图,其中 x 轴是日期数据。刻度标签通常很长,它有助于在底部子图上旋转它们并在其他子图上关闭它们,以及关闭 xlabels。

如果您只想旋转右下角子图的 xticklabels,请使用

plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment

这会将刻度标签旋转 30 度并将它们右对齐(与使用 时的结果相同autofmt_xdate)用于右下角的子图,而其他两个子图保持不变。

于 2013-07-02T16:35:46.290 回答