当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Changing the MinorLocator when plotting with AxisGrid

问题描述:

I am using this function from the documentation to make a nice plot with matplotlib.

def demo_grid_with_single_cbar(fig):

"""

A grid of 2x2 images with a single colorbar

"""

grid = AxesGrid(fig, 132, # similar to subplot(132)

nrows_ncols = (2, 2),

axes_pad = 0.0,

share_all=True,

label_mode = "L",

cbar_location = "top",

cbar_mode="single",

)

Z, extent = get_demo_image()

for i in range(4):

im = grid[i].imshow(Z, extent=extent, interpolation="nearest")

#plt.colorbar(im, cax = grid.cbar_axes[0])

grid.cbar_axes[0].colorbar(im)

for cax in grid.cbar_axes:

cax.toggle_label(False)

# This affects all axes as share_all = True.

grid.axes_llc.set_xticks([-2, 0, 2])

grid.axes_llc.set_yticks([-2, 0, 2])

I would like to change the minorlocator. but I really have no clue where to change the code.

If it where just a single graphics I would manipulate the axes object. But with AxesGrid I am lost.

majorLocator = MultipleLocator(50)

majorFormatter = FormatStrFormatter('%d')

minorLocator = MultipleLocator(10)

ax.yaxis.set_major_locator(majorLocator)

ax.yaxis.set_major_formatter(majorFormatter)

ax.yaxis.set_minor_locator(minorLocator)

EDIT after the question was solved.

The result should look like the following picture, notice the minor ticks!

The result should look like this image

http://i.stack.imgur.com/o1YPK.png

Maybe someone else can append it. I cannot because my reputation credits do not suffice.

网友答案:

Note the warning on main page. If you have a new enough version of matplotlib this works.

from mpl_toolkits.axes_grid1 import ImageGrid

def get_demo_image():
    import numpy as np
    from matplotlib.cbook import get_sample_data
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
    z = np.load(f)
    # z is a numpy array of 15x15
    return z, (-3,4,-4,3)

def demo_grid_with_single_cbar(fig):
    """
    A grid of 2x2 images with a single colorbar
    """
    grid = ImageGrid(fig, 132, # similar to subplot(132)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.0,
                    share_all=True,
                    label_mode = "L",
                    cbar_location = "top",
                    cbar_mode="single",
                    )

    Z, extent = get_demo_image()
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
    #plt.colorbar(im, cax = grid.cbar_axes[0])
    grid.cbar_axes[0].colorbar(im)

    for cax in grid.cbar_axes:
        cax.toggle_label(False)

    # This affects all axes as share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])

    for i in range(4):
        ax = grid[i]
        ax.yaxis.set_minor_locator(MultipleLocator(.5))
分享给朋友:
您可能感兴趣的文章:
随机阅读: