Matplotlib子图 - 完全摆脱勾选标签

Pal*_*l86 16 matplotlib subplot axis-labels

在Matplotlib中创建一个子图数组时,有没有办法完全摆脱刻度标签?我目前需要根据绘图对应的较大数据集的行和列来指定每个绘图.我试图使用ax.set_xticks([])和类似的y轴命令,但无济于事.

我认识到,想要制作没有轴数据的情节可能是一个不寻常的要求,但这就是我所需要的.我需要它自动应用于数组中的所有子图.

DrV*_*DrV 22

你有正确的方法.也许你没有应用set_xticks到正确的轴.

一个例子:

import matplotlib.pyplot as plt
import numpy as np

ncols = 5
nrows = 3

# create the plots
fig = plt.figure()
axes = [ fig.add_subplot(nrows, ncols, r * ncols + c) for r in range(0, nrows) for c in range(0, ncols) ]

# add some data
for ax in axes:
    ax.plot(np.random.random(10), np.random.random(10), '.')

# remove the x and y ticks
for ax in axes:
    ax.set_xticks([])
    ax.set_yticks([])
Run Code Online (Sandbox Code Playgroud)

这给出了:

在此输入图像描述

请注意,每个轴实例都存储在list(axes)中,然后可以轻松地操作它们.像往常一样,有几种方法可以做到这一点,这只是一个例子.

  • 单线是`plt.setp(axes,xticks = [],yticks = [])`。 (3认同)

Ben*_*Ben 13

子图的命令相同

In [1]: fig = plt.figure()

In [2]: ax1 = fig.add_subplot(211)

In [3]: ax2 = fig.add_subplot(212)

In [4]: ax1.plot([1,2])
Out[4]: [<matplotlib.lines.Line2D at 0x10ce9e410>]

In [5]: ax1.tick_params(
   ....:     axis='x',          # changes apply to the x-axis
   ....:     which='both',      # both major and minor ticks are affected
   ....:     bottom='off',      # ticks along the bottom edge are off
   ....:     top='off',         # ticks along the top edge are off
   ....:     labelbottom='off'  # labels along the bottom edge are off)
   ....:)

In [6]: plt.draw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Roy*_*rot 6

比@DrV的答案更简洁,重新混合@mwaskom的注释,完整而完整的单行代码以摆脱所有子图中的所有轴:

# do some plotting...
plt.subplot(121),plt.imshow(image1)
plt.subplot(122),plt.imshow(image2)
# ....

# one liner to remove *all axes in all subplots*
plt.setp(plt.gcf().get_axes(), xticks=[], yticks=[]);
Run Code Online (Sandbox Code Playgroud)