matplotlib:直方图和bin标签 [英] matplotlib: histogram and bin labels

查看:263
本文介绍了matplotlib:直方图和bin标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to plot a histogram with bar chart, and I'm having difficulties figuring out how to align the x-axis labels with the actual bins. The code below generates the following plot:

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

as you can see, the end of each x-label is not aligned to the center of its bin. The way i'm thinking about this is: when i apply a 45-degree rotation, the label pivots around its geometrical center. I was wondering if it's possible to move the pivot up to the top of the label. (Or simply translate all the labels slightly left.)

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")

输出为:

这篇关于matplotlib:直方图和bin标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆