matplotlib boxplot xticks向y轴移动

时间:2015-10-20 11:34:16

标签: python numpy pandas matplotlib

我是matplotlib的初学者,并试图为我使用pandas提取的数据绘制一个boxplot。

我成功获得了boxplot,它看起来非常像我想要的除了xtics。这些xtics正在转向yaxis。我不知道为什么。

以下是我的示例数组:

In [74]: print occ

[[0.40000000000000002, 25.199999999999999, 2.0, 0.40000000000000002, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 21.199999999999999, 0.0, 0.0, 26.800000000000001, 12.800000000000001, 0.40000000000000002, 0.40000000000000002, 0.40000000000000002, 0.0], [0.0, 31.199999999999999, 0.0, 0.0, 21.600000000000001, 0.0, 0.0, 0.40000000000000002, 0.0, 4.7999999999999998], [0.0, 32.0, 0.0, 0.0, 23.199999999999999, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 37.350000000000001, 0.0, 0.0, 13.65, 0.0, 0.0, 0.0, 0.0, 1.2]]

In [78]: dfAll.pair
​
Out[78]:
0    GLU18-Side-ASN50-Side
1    ASN50-Side-ARG21-Side
2    GLN14-Side-ASN42-Side
3    ASN42-Side-GLN14-Side
4    GLN14-Side-ARG43-Side
5    SER46-Side-ARG15-Side
6    ASN42-Side-ARG15-Side
7    GLU11-Side-ASN42-Side
8    ALA39-Main-GLN14-Side
9    GLU18-Side-SER46-Side
Name: pair, dtype: object

以下是我的matplotlib boxplot代码:

plt.boxplot(np.array(occ),  showmeans=True, whis="range", showbox=True, labels=dfAll.pair)
plt.xticks(range(0,(dfAll.pair.shape[0])), dfAll.pair, rotation='vertical')
plt.show()

Box Plot Image with xticks shifted  towards y axis.

知道为什么会这样,以及如何获得正确的xtics?

1 个答案:

答案 0 :(得分:2)

错误在于这一行:

table

您明确指定plt.xticks(range(0,(dfAll.pair.shape[0])), dfAll.pair, rotation='vertical') 作为起点的事实导致了问题。将其更改为:

0

仅作为旁注,因为您的值为plt.xticks(range(1,(dfAll.pair.shape[0]+1)), dfAll.pair, rotation='vertical') ,我会将y=0扩展为ylim()