Remove xticks and yticks of gridspec matlibplot plot

Viewed 1188

I am trying to remove all the labels and x/yticks from a gridspec plot that I have set up.

The below graph is my current output but I need to remove all the labels and x-ticks and y-ticks so they are basically empty boxes. I just can't do it for some reason.

Graph

So this is my code here that I am using.

fig = plt.figure()
gs = fig.add_gridspec(2, 9)

# Query image
ax1 = fig.add_subplot(gs[:, 0])
#Positive image
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[0, 2])
ax4 = fig.add_subplot(gs[0, 3])
ax5 = fig.add_subplot(gs[0, 4])
# Negative images
ax6 = fig.add_subplot(gs[1, 1])
ax7 = fig.add_subplot(gs[1, 2])
ax8 = fig.add_subplot(gs[1, 3])
ax9 = fig.add_subplot(gs[1, 4])

# all_axes = fig.get_axes()
# # show only the outside spines
# for ax in all_axes:
#     for sp in ax.spines.values():
#         sp.set_visible(False)
#     if ax.is_first_row():
#         ax.spines['top'].set_visible(True)
#     if ax.is_last_row():
#         ax.spines['bottom'].set_visible(True)
#     if ax.is_first_col():
#         ax.spines['left'].set_visible(True)
#     if ax.is_last_col():
#         ax.spines['right'].set_visible(True)
plt.tick_params(axis='x',
           which='both',
           bottom=False,
           top = False,
           labelbottom=False)
#plt.axis('off')
#plt.xticks([], [])
#plt.xticks.remove
#plt.yticks.remove
plt.show()

I have tried several approaches but still no good.

1 Answers

You should be using matplotlib.axes.Axes.tick_params() instead of matplotlib.pyplot.tick_params(). The latter will apply only to the 'current', in this case the most recently created, axes instance - the former allows you to apply the tick params to any axes instance. Consider:

for ax in fig.get_axes():
    ax.tick_params(bottom=False, labelbottom=False, left=False, labelleft=False)

Which will produce

enter image description here

Related