关于这些子图中的重叠标签,我该怎么办?

时间:2013-03-15 20:35:28

标签: python matplotlib

下面是我用matplotlib创建的图。问题非常明显 - 标签重叠,整个事情是一个难以理解的混乱。

enter image description here

我尝试为每个子图调用tight_layout,但这会使我的ipython-notebook内核崩溃。

我该怎么做才能修复布局?可接受的方法包括修复每个子图的xlabel,ylabel和title,但另一种(也许更好的)方法是为整个图形提供单个xlabel,ylabel和title。

这是我用来生成上述子图的循环:

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()

更新

好的,所以ImageGrid似乎是要走的路,但我的身材仍然看起来有些不稳定:

enter image description here

这是我使用的代码:

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()

1 个答案:

答案 0 :(得分:3)

您想要ImageGridtutorial)。

直接从该链接中取出的第一个示例(并进行了轻微修改):

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()