未绘制最后一个小刻度 - Pyplot

tsc*_*ppi 2 python matplotlib

所以我试图在共享x轴的同一图中绘制两个子图.但是,我无法得到它来绘制最后一个小xtick.我不知道这种行为来自哪里,但我设法用随机数据重现它.

使用的系统是python2.7和matplotlib v1.2.1

所以这里是我的最小错误再现示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator

xdat = np.linspace(0,6.6,endpoint=True)
ydat1 = np.random.rand(50)*500
ydat2 = np.random.rand(50)*4

fig = plt.figure(figsize=(6,8), dpi=72)
gs = gridspec.GridSpec(2,1, height_ratios=[3,1])
fig.subplots_adjust(hspace=0.0)
ax1 = plt.subplot(gs[0])

ax1.plot(xdat, ydat1)
ax1.set_xlim(0,6.6)
ax1.set_xticks(range(0,8,1))
ax1.minorticks_on()
[label.set_visible(False) for label in ax1.get_xticklabels() ]  # Make tick labels invisible

ax2 = plt.subplot(gs[1], sharex=ax1)
ax2.plot(xdat, ydat2, 'r-')
ax2.yaxis.set_major_locator(MaxNLocator(nbins=5, steps=[1,2,4,5,10], symmetric=False, prune='upper'))
plt.show()
Run Code Online (Sandbox Code Playgroud)

我得到了以下图像: 缺少最后一个小刻度的示例图

我不知道我是否发现了一个错误,或者是否有一种简单的方法来缓解这个问题(即更新matplotlib).

CT *_*Zhu 5

还没有找到bug来自哪里,但版本1.3.1具有相同的行为.

解决方法是手动设置次要刻度,添加a ax2.xaxis.set_ticks(np.hstack((ax2.xaxis.get_ticklocs(minor=True), 6.4)), minor=True),6.4最后一个小刻度. 在此输入图像描述

或者您可以强制xlim略大于默认值,最后一个勾号将会出现.ax2.set_xlim((0,6.6)).默认是(0.0, 6.5999999999999996).

我想这可以被视为一个bug.