简体   繁体   English

Python如何为matplotlib图设置轴

[英]Python How to set axes for a matplotlib plot

Hi for the matplotlib plot below I want to set the axes titles such that they show that the x-axis values run from 您好,下面的matplotlib图我想设置轴标题,以使它们显示x轴值从

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

and the y-axis values run from y轴值从

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

The graph I want to display them on is: 我想在上面显示的图形是:

需要更改轴的图形

The code for the graph is below: 该图的代码如下:

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

You can use a range with a stepsize to label every 5th cell: 您可以使用带有步进大小的range来标记第5个单元格:

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

For example, 例如,

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM