Python如何为matplotlib图设置轴

时间:2016-04-23 23:33:50

标签: python matplotlib axes

嗨,对于下面的matplotlib图,我想设置轴标题,以便它们显示x轴值从

开始
2**-5, 2**-4, 2**-3,..., 2**14, 2**15

,y轴值从

开始
2**-15, 2**-14,...., 2**4, 2**5

我想要显示的图表是:

The graph with the axes that needs changing

图表的代码如下:

from matplotlib import pyplot
import matplotlib as mpl

import numpy as np


zvals = 100*np.random.randn(21, 21)
fig = pyplot.figure(2)

cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                           ['blue','green','brown'],
                                           256)

img2 = pyplot.imshow(zvals,interpolation='nearest',
                    cmap = cmap2,
                    origin='lower')

pyplot.colorbar(img2,cmap=cmap2)
pyplot.show()

1 个答案:

答案 0 :(得分:2)

您可以使用带有步长的range标记每个第5个单元格:

locs = range(0, N, 5)
ax.set(xticks=locs, xlabels=...)

例如,

from matplotlib import pyplot as plt
from matplotlib import colors as mcolors
import numpy as np


N = 21
zvals = 100*np.random.randn(N, N)
fig = plt.figure(2)
ax = fig.add_subplot(111)
cmap2 = mcolors.LinearSegmentedColormap.from_list(
    'my_colormap', ['blue','green','brown'], 256)

img2 = plt.imshow(zvals,interpolation='nearest',
                  cmap=cmap2, origin='lower')
plt.colorbar(img2, cmap=cmap2)
step = 5
locs = range(0, N, step)
ax.set(
    xticks=locs,
    xticklabels=['$2^{{{}}}$'.format(i-5) for i in locs],
    yticks=locs,
    yticklabels=['$2^{{{}}}$'.format(i-15) for i in locs])
plt.show()

enter image description here