Below is a figure I created with matplotlib. The problem is pretty obvious -- the labels overlap and the whole thing is an unreadable mess.
I tried calling tight_layout
for each subplot, but this crashes my ipython-notebook kernel.
What can I do to fix the layout? Acceptable approaches include fixing the xlabel, ylabel, and title for each subplot, but another (and perhaps better) approach would be to have a single xlabel, ylabel and title for the entire figure.
Here's the loop I used to generate the above subplots:
for i, sub in enumerate(datalist):
subnum = i + start_with
subplot(3, 4, i)
# format data (sub is a PANDAS dataframe)
xdat = sub['x'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
ydat = sub['y'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
# plot
hist2d(xdat, ydat, bins=1000)
plot(0, 0, 'ro') # origin
title('Subject {0} in-Trial Gaze'.format(subnum))
xlabel('Horizontal Offset (degrees visual angle)')
ylabel('Vertical Offset (degrees visual angle)')
xlim([-.005, .005])
ylim([-.005, .005])
# tight_layout # crashes ipython-notebook kernel
show()
Update:
Okay, so ImageGrid
seems to be the way to go, but my figure is still looking a bit wonky:
Here's the code I used:
fig = figure(dpi=300)
grid = ImageGrid(fig, 111, nrows_ncols=(3, 4), axes_pad=0.1)
for gridax, (i, sub) in zip(grid, enumerate(eyelink_data)):
subnum = i + start_with
# format data
xdat = sub['x'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
ydat = sub['y'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
# plot
gridax.hist2d(xdat, ydat, bins=1000)
plot(0, 0, 'ro') # origin
title('Subject {0} in-Trial Gaze'.format(subnum))
xlabel('Horizontal Offset\n(degrees visual angle)')
ylabel('Vertical Offset\n(degrees visual angle)')
xlim([-.005, .005])
ylim([-.005, .005])
show()
You want ImageGrid
(tutorial).
First example lifted directly from that link (and lightly modified):
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
im = np.arange(100)
im.shape = 10, 10
fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols = (2, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
aspect=False, # do not force aspect='equal'
)
for i in range(4):
grid[i].imshow(im) # The AxesGrid object work as a list of axes.
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With