astrobel
astrobel

Reputation: 55

python matplotlib gridspec, unwanted arbitrary axis labels

I have some code to plot a grid, with the data in each cell being distinct and having a very specific position. The easiest way I found to do this was to create the grid with gridspec and use it to precisely position my subplots, however I'm having a problem where the overall grid is labelled from 0 to 1 along each axis. This happens every time, even when the dimensions of the grid are changed. Obviously these numbers have no relevance to my data, and as what I am aiming to display is qualitative rather than quantitative I would like to remove all labels from this plot entirely.

Here is a link to an image with an example of my problem

And here is the MWE that I used to create that image:

import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt

# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))

# plotting
fig = plt.figure(1)
gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')

for (j, k), img in np.ndenumerate(table):
   ax = fig.add_subplot(gs[x - j - 1, k])
   ax.set_xticklabels('')
   ax.set_yticklabels('')

plt.show()

I have not been able to find note of anything like this problem, so any help would be greatly appreciated.

Upvotes: 3

Views: 2185

Answers (1)

Serenity
Serenity

Reputation: 36635

If you just want to draw a grid over the plot, use this code:

import numpy as np
import matplotlib.pyplot as plt

# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))

# plotting
fig = plt.figure(1)
plt.title('Example Plot')

plt.gca().xaxis.grid(True, color='darkgrey', linestyle='-')
plt.gca().yaxis.grid(True, color='darkgrey', linestyle='-')
plt.show()

Another variant is used gridspec:

...
# hide ticks of main axes
ax0 = plt.gca()
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])

gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')

for (j, k), img in np.ndenumerate(table):
    ax = fig.add_subplot(gs[x - j - 1, k])
    # hide ticks of gribspec axes
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])

enter image description here

Upvotes: 2

Related Questions