Matplotlib x轴默认标签不会消失

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

我正在处理数据集,但结果有些奇怪。我已经创建了一个网格,并且试图使x轴标签仅显示在底部图表中并显示年份。但是,默认的a-xis标签保留在后台。我尝试了许多不同的方法,但是没有运气。有什么用?

代码:

fig, ((ax1, ax2, ax3), 
      (ax4, ax5, ax6), 
      (ax7, ax8, ax9), 
      (ax10, ax11, ax12), 
      (ax13, ax14, ax15), 
      (ax16, ax17, ax18)) = plt.subplots(6, 3, figsize=(30, 60),
                                          sharex=True, sharey=True)

和...

image

结果:

红线指向小虫子...

image

python matplotlib axis-labels
1个回答
0
投票

看来您正在创建两组轴,一次在plt.subplots()调用中,然后在随后的fig.add_subplot()调用中再次创建。

您可以通过执行以下操作来修改第一个调用以仅创建图形:

fig = plt.figure(figsize = (30, 60))

...,然后在添加子图的调用上进行轴共享:

for sp in range(0, 16, 3):
    ax = fig.add_subplot(6, 3, sp, sharex = True, sharey = True)

或者,如果要先创建所有子图,然后在for循环中访问它们,则:

fig, axes = plt.subplots(6, 3, figsize = (30, 60), sharex = True, sharey = True) 
for sp in range(0, 16, 3):
    ax = axes[sp]
© www.soinside.com 2019 - 2024. All rights reserved.