matplotlib xtick标签未对齐 [英] matplotlib xtick labels not aligned

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

问题描述

我创建了一个非常简单的堆叠条形图,但是由于某种原因,我的x-tick标签偏离了中心.我尝试使用一些tick.label.set_属性将它们排列成一行,但无济于事.有什么建议?谢谢!

I created a pretty simple stacked bar chart, but for some reason my x-tick labels are off centered. I tried using some tick.label.set_ properties to line them up be to no avail. Any suggestions? Thanks!

代码:

bottom = np.vstack((np.zeros((data.shape[1],), dtype=data.dtype), np.cumsum(data, axis=0) [:-1]))
for dat, col, bot, lab in zip(data, colors, bottom, labels):
    ax1.bar(ind, dat, color=col, bottom = bot, alpha = .7, label = lab)

xticks([z for z in range(0,len(income_brackets))],[(str("{0:.0f}".format(k/1000)) + '-' + str("{0:.0f}".format(l/1000))) for k,l in income_brackets])
for tick in ax1.xaxis.get_major_ticks():
    tick.label.set_fontsize(12)
    tick.label.set_horizontalalignment('left')

图片:

推荐答案

使用 align="center" ,默认情况下它们在边缘对齐.这是一个最小的工作示例:

Use align="center" in your bar plot, by default they line up on the edges. Here is a minimal working example:

import pylab as plt

X = [1,2,3,4]
Y = [1,4,3,2]

fig, axes = plt.subplots(2, 1)
ax1, ax2 = axes

ax1.bar(X,Y,alpha=.5)
ax1.set_xticks(X)

ax2.bar(X,Y,align="center",alpha=.5)
ax2.set_xticks(X)           

plt.show()

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

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