【问题标题】:Last minor tick not drawn - Pyplot未绘制最后一个小刻度 - Pyplot
【发布时间】: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


    【解决方案1】:

    目前还无法找到错误的来源,但版本1.3.1 具有相同的行为。

    一种解决方法是手动设置次要刻度,方法是添加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)

    我猜它可以被认为是一个错误。

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多