Matplotlib:旋转xticks时,标签之间的间隙不均匀

2024-10-01 00:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

这很奇怪。旋转-90º时,所有标签的间距相等,并位于其位置:

feat_names = ["totalUniqueCustomerPhoneNumberLocal_f",
                "userIdentitty_f",
                "EMAIL_WEEKS_1_f",
                "length_f",
                "totalValidations_f",
                "amount_per_purchase_k",
                "amount_f"]

X1 = np.array([0.18790969, 0.18208303, 0.15003641, 0.11143482, 0.07428987,
       0.06846322, 0.06773489])

plt.figure()
plt.title("Feature importances")
plt.bar(range(X1.shape[0]), X1,color="r", align="center")
plt.xticks(range(X1.shape[0]), feat_names, rotation = -90)
plt.xlim([-1, X1.shape[0]])
plt.show()

result of script

但是,我需要将它们旋转-45º,结果如下:

feat_names = ["totalUniqueCustomerPhoneNumberLocal_f",
                "userIdentitty_f",
                "EMAIL_WEEKS_1_f",
                "length_f",
                "totalValidations_f",
                "amount_per_purchase_k",
                "amount_f"]

X1 = np.array([0.18790969, 0.18208303, 0.15003641, 0.11143482, 0.07428987,
       0.06846322, 0.06773489])

plt.figure()
plt.title("Feature importances")
plt.bar(range(X1.shape[0]), X1,color="r", align="center")
plt.xticks(range(X1.shape[0]), feat_names, rotation = -45)
plt.xlim([-1, X1.shape[0]])
plt.show()

result of script2

当标签旋转-45º时,如何使标签开始与条的中心对齐

谢谢


Tags: namesemailrangeplt标签amountlengthx1