为什么有些地块有网格线,而另一些地块则没有?

问题描述 投票:0回答:1

考虑这个 MWE,

from matplotlib import pyplot
pyplot.style.use('classic')
pyplot.rcParams.update( {
    'text.usetex': True,
    "font.family": "serif",
    'text.latex.preamble': r'\usepackage{amsmath, amssymb, mlmodern}', } )
import numpy


fig, ax = pyplot.subplots(3,3)
fig.tight_layout()
fig.subplots_adjust(hspace=0.1, wspace=0.1,
    left=0.09, right=.95, top=.95, bottom=.09)

x = numpy.linspace(0, 8, 100)

for i in range(3):
    for j in range(3):
        ax[i,j].plot(x, numpy.sin((1+j)*x+numpy.pi*i), )
        ax[i,j].grid(which='both')  # <----------------- I added grid here
        if i!=2: ax[i,j].set_xticks([])
        if j==1: ax[i,j].set_yticks([])
        if j==2: ax[i,j].yaxis.tick_right()

ax[0,0].set_ylabel('$\phi=0$')
ax[1,0].set_ylabel('$\phi=\pi$')
ax[2,0].set_ylabel('$\phi=2\pi$')

ax[2,0].set_xlabel('$f = 1$')
ax[2,1].set_xlabel('$f = 2$')
ax[2,2].set_xlabel('$f = 3$')

pyplot.savefig('waves.png')

产生以下情节,

我不明白为什么matplotlib有(0,0)和(0,2)轴的网格线,(0,1)轴的垂直网格线,(1,0)轴的水平网格线,(1, 2)、(2,0) 和 (2,2),并且轴 (1,1) 和 (2,1) 没有网格线。

如何确保所有轴的网格类似于 (0,0)?谢谢你。

python matplotlib plot plot-grid
1个回答
0
投票

您的问题是由于去除蜱虫造成的。相反,您可以删除勾选标签

更换:

        if i!=2: ax[i,j].set_xticks([])
        if j==1: ax[i,j].set_yticks([])

与:

        if i!=2: ax[i,j].set_xticklabels([])
        if j==1: ax[i,j].set_yticklabels([])

输出:

© www.soinside.com 2019 - 2024. All rights reserved.