2016-04-23 132 views
1

喜为matplotlib情节设置轴下面我想设置轴标题,使得它们表明,X轴的值从的Python如何为matplotlib阴谋

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

你检查过了吗? (你可能需要标签):http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html – Adib

+0

这里可能有重复吗? http://stackoverflow.com/questions/3100985/plot-with-custom-text-for-x-axis-points – Adib

+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