python - Matplotlib:当旋转 xticks 出现标签之间不均匀的间隙时
问题描述
这很奇怪。旋转 -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()
但是,我需要它们旋转 -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()
关于如何使标签在旋转-45º时开始与条形中心对齐的任何想法?
谢谢
解决方案
水平对齐是通过horizontalalignment
或其别名完成的ha
,
plt.xticks(..., rotation = -45, ha="left")
推荐阅读
- android - 检索启动意图的堆栈跟踪
- pine-script - 如何仅在条件为真的第一根蜡烛上绘制形状?
- r - 使用大型数据表提高查找性能
- python - 如何让 pipenv 在多个版本的 brew python 中工作
- google-app-engine - 将 Google AppEngine 从 Go 1.9 迁移到 Go 1.11 的 app.yaml api_version 问题
- python - 在 tf.unique_with_counts() 之后将标签映射到计数
- jquery - 日期选择器将格式更改为数字
- java - 使用 Saxon/XQuery 设置 URI 或目录解析器
- html - 带有 img-thumbnail 的下拉项目超出其容器
- php - Composer 显示 2 个兼容包之间的冲突(原始)