rotation - matplotlib:直方图和bin标签

标签 rotation matplotlib histogram bar-chart axis-labels

我正在尝试用条形图绘制直方图,并且在弄清楚如何将x轴标签与实际纸箱对齐时遇到困难。下面的代码生成以下图:

如您所见,每个x标签的末尾都未对齐其容器的中心。我考虑的方式是:当我进行45度旋转时,标签围绕其几何中心旋转。我想知道是否有可能将枢轴移到标签的顶部。 (或者只是将所有标签稍微向左平移。)

import matplotlib.pyplot as plt
import numpy as np

#data
np.random.seed(42)
data = np.random.rand(5)
names = ['A:GBC_1233','C:WERT_423','A:LYD_342','B:SFS_23','D:KDE_2342']

ax = plt.subplot(111)
width=0.3
bins = map(lambda x: x-width/2,range(1,len(data)+1))
ax.bar(bins,data,width=width)
ax.set_xticks(map(lambda x: x, range(1,len(data)+1)))
ax.set_xticklabels(names,rotation=45)

plt.show()

最佳答案

用:

ax.set_xticklabels(names,rotation=45, rotation_mode="anchor", ha="right")

输出为:

关于rotation - matplotlib:直方图和bin标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938532/

相关文章:

r - 将时间线上的时间跨度可视化为水平直方图/条形图

python - 尝试使用 numpy 在字典中创建给定值的直方图,但并非所有值都显示出来

ios - 保存和恢复 UIImageView 的旋转和大小

android - 旋转图像以及触摸以在android中固定点

python - 在 Matplotlib 中创建颜色图图例

python - 如何使用 curve_fit 和线性线函数使用散点图绘制下边界?

python - 使用 Matplotlib 和 mpld3 在 Web 浏览器上生成多个图形

c - 向左旋转并向右旋转以使用 C 中的(带符号短)强制转换进行符号扩展

html - css3 rotate ul 在 Internet Explorer 中消失

vba - Excel 直方图 - 分布线