gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:55:20 24 4
gpt4 key购买 nike

所以我试图在同一个图中绘制两个共享 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()

我得到了下图: Example plot where last minor tick is missing

我不知道我是否发现了错误,或者是否有缓解此问题的简单方法(即更新 matplotlib)。

最佳答案

还不能找到错误的来源,但是版本 1.3.1 具有相同的行为。

解决方法是通过添加 ax2.xaxis.set_ticks(np.hstack((ax2.xaxis.get_ticklocs(minor=True), 6.4)), minor=True 手动设置次刻度),其中 6.4 是最后一个小刻度。 enter image description here

或者您可以强制 xlim 比默认值稍微大一点,最后一个刻度就会出来。 ax2.set_xlim((0,6.6))。默认值为 (0.0, 6.5999999999999996)

我想这可以被认为是一个错误。

关于python - 未绘制最后一个小刻度 - Pyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22843695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com