【发布时间】:2014-04-04 00:45:01
【问题描述】:
所以我试图在同一个图中绘制两个共享 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()
我得到以下图像:
我不知道我是否发现了一个错误,或者是否有一种简单的方法可以缓解这个问题(即更新 matplotlib)。
【问题讨论】:
标签: python matplotlib