Python 使用MaxNLocator更改子地块上的记号数-记号消失

Python 使用MaxNLocator更改子地块上的记号数-记号消失,python,matplotlib,axis-labels,subplot,Python,Matplotlib,Axis Labels,Subplot,我在Python3(我想)中使用matplotlib在for循环中创建了几个子包,我想设置ytick/xtick的数量,因为每个循环的最大值和最小值都不同。 我已经尝试过使用MaxNLocator,正如这个答案中所建议的那样 (很抱歉,我现在要问另一个问题,但我无法对这里给出的答案发表评论): 但这对我不起作用。yticklabels和ytick在for循环中的子批次上完全消失,我得到了两个xtick,尽管我请求了五个。for循环外部的一个子批次仍然有yticklabels,但刻度数与请求的刻度

我在Python3(我想)中使用matplotlib在
for
循环中创建了几个子包,我想设置
ytick/xtick
的数量,因为每个循环的最大值和最小值都不同。 我已经尝试过使用MaxNLocator,正如这个答案中所建议的那样 (很抱歉,我现在要问另一个问题,但我无法对这里给出的答案发表评论):

但这对我不起作用。
yticklabels
ytick
for
循环中的子批次上完全消失,我得到了两个
xtick
,尽管我请求了五个。
for
循环外部的一个子批次仍然有
yticklabels
,但刻度数与请求的刻度数不同。 我也没有使用对数轴! 如果有人有主意的话,我会很好,如何解决这个问题!:)

我的代码的结构是:

f = plt.figure(figsize =(13,10),dpi=300)

yticks = ticker.MaxNLocator(4)    # number of yticks
xticks = ticker.MaxNLocator(5)    # number of xticks
yfmt = md.DateFormatter('%d.%m.\n%H:%M')    # format of date

for n,m in enumerate (['20-25cm','15-20cm','10-15cm','5-10cm','0-5cm']):
    ax = f.add_subplot(6,1,-n+6)    # subplots number 6 to number 2

    ax.yaxis.set_major_locator(yticks)    # <-- ?
    ax.xaxis.set_major_locator(xticks)    # <-- ?
    ax.xaxis.set_major_formatter(yfmt)    # format for date on xaxis
    ax.xaxis.set_tick_params(labelsize=20)    
    ax.yaxis.set_tick_params(labelsize=20)
    if n !=0:
        ax.set_xticklabels([])    # only put xtlabels at the bottom subplot

    Y = dictionary[data][n][:]    
    #Y is a np.array with floats from a dictionary

    X = dictionary[date][n][:]    
    #X is a np.array containing datetime objects from the same dictionary

    plt.plot(X[-250:-1],Y[-250:-1], label = m)


ax1 = f.add_subplot(6,1,1)    # subplot number 1 outside the loop
ax1.xaxis.set_major_formatter(yfmt)
ax1.xaxis.set_tick_params(labelsize=20)    
ax1.yaxis.set_tick_params(labelsize=20)
ax1.yaxis.set_major_locator(yticks)    # <-- ?
ax1.xaxis.set_major_locator(xticks)    # <-- ?
ax1.set_xticklabels([])

plt.scatter(X[-250:-1],poolA[-250:-1])    # poolA,B,C are just np.arrays
plt.hold(True)
plt.scatter(X[-250:-1],poolB[-250:-1])
plt.hold(True)
plt.scatter(X[-250:-1],poolC[-250:-1]) 

plt.savefig('figure.pdf',format = 'pdf', dpi=f.dpi)
f=plt.图(figsize=(13,10),dpi=300)
yticks=ticker.MaxNLocator(4)#yticks的数量
xticks=ticker.MaxNLocator(5)#xticks的数量
yfmt=md.DateFormatter(“%d.%m.\n%H:%m”)#日期格式
对于n,枚举中的m(['20-25cm','15-20cm','10-15cm','5-10cm','0-5cm']):
ax=f.将子批次(6,1,-n+6)#子批次编号6添加到编号2

ax.yaxis.set_major_locator(yticks)#看起来您正在处理xaxis上的日期。这使得事情更加复杂。请提供要处理的问题的(可运行代码)。还请注意,
MaxNLocator
设置最大存储箱数量。因此,你最终可能得到的数字总是小于给定的数字(它实际上是maxN,而不是N本身)。这使得事情更加复杂。请提供要处理的问题的(可运行代码)。还请注意,
MaxNLocator
设置最大存储箱数量。因此,最终的结果可能总是小于给定的数字(它实际上是maxN,而不是N本身)。
f = plt.figure(figsize =(13,10),dpi=300)

yticks = ticker.MaxNLocator(4)    # number of yticks
xticks = ticker.MaxNLocator(5)    # number of xticks
yfmt = md.DateFormatter('%d.%m.\n%H:%M')    # format of date

for n,m in enumerate (['20-25cm','15-20cm','10-15cm','5-10cm','0-5cm']):
    ax = f.add_subplot(6,1,-n+6)    # subplots number 6 to number 2

    ax.yaxis.set_major_locator(yticks)    # <-- ?
    ax.xaxis.set_major_locator(xticks)    # <-- ?
    ax.xaxis.set_major_formatter(yfmt)    # format for date on xaxis
    ax.xaxis.set_tick_params(labelsize=20)    
    ax.yaxis.set_tick_params(labelsize=20)
    if n !=0:
        ax.set_xticklabels([])    # only put xtlabels at the bottom subplot

    Y = dictionary[data][n][:]    
    #Y is a np.array with floats from a dictionary

    X = dictionary[date][n][:]    
    #X is a np.array containing datetime objects from the same dictionary

    plt.plot(X[-250:-1],Y[-250:-1], label = m)


ax1 = f.add_subplot(6,1,1)    # subplot number 1 outside the loop
ax1.xaxis.set_major_formatter(yfmt)
ax1.xaxis.set_tick_params(labelsize=20)    
ax1.yaxis.set_tick_params(labelsize=20)
ax1.yaxis.set_major_locator(yticks)    # <-- ?
ax1.xaxis.set_major_locator(xticks)    # <-- ?
ax1.set_xticklabels([])

plt.scatter(X[-250:-1],poolA[-250:-1])    # poolA,B,C are just np.arrays
plt.hold(True)
plt.scatter(X[-250:-1],poolB[-250:-1])
plt.hold(True)
plt.scatter(X[-250:-1],poolC[-250:-1]) 

plt.savefig('figure.pdf',format = 'pdf', dpi=f.dpi)