代码之家  ›  专栏  ›  技术社区  ›  attwad

如何使用imshow使用Matplotlib绘制具有非线性y轴的图像?

  •  11
  • attwad  · 技术社区  · 16 年前
    4 回复  |  直到 16 年前
        1
  •  8
  •   RedGlyph sumit sonawane    16 年前

    你试过变换轴吗?例如:

    ax = subplot(111)
    ax.yaxis.set_ticks([0, 2, 4, 8])
    imshow(data)
    

    这意味着,对于不存在的坐标,数据中必须存在空白,除非有一种方法可以提供变换函数,而不仅仅是列表(从未尝试过)。

    :

    假设你的数据在一个数组中, a

    class arr(object):
        @staticmethod
        def mylog2(x):
            lx = 0
            while x > 1:
                x >>= 1
                lx += 1
            return lx
        def __init__(self, array):
            self.array = array
        def __getitem__(self, index):
            return self.array[arr.mylog2(index+1)]
        def __len__(self):
            return 1 << len(self.array)
    

    基本上,它将用 mylog2

    然后将你的数组映射到这个数组,它不会在实例中创建副本,而是一个本地引用:

    b = arr(a)
    

    ax = subplot(111)
    ax.yaxis.set_ticks([16, 8, 4, 2, 1, 0])
    axis([-0.5, 4.5, 31.5, 0.5])
    imshow(b, interpolation="nearest")
    

    alt text http://img691.imageshack.us/img691/8883/clipboard01f.png

        2
  •  4
  •   Brandon Mechtley    14 年前

    imshow specgram

    PyWavelets :

    from pylab import *
    import pywt
    import scipy.io.wavfile as wavfile
    
    # Find the highest power of two less than or equal to the input.
    def lepow2(x):
        return 2 ** floor(log2(x))
    
    # Make a scalogram given an MRA tree.
    def scalogram(data):
        bottom = 0
    
        vmin = min(map(lambda x: min(abs(x)), data))
        vmax = max(map(lambda x: max(abs(x)), data))
    
        gca().set_autoscale_on(False)
    
        for row in range(0, len(data)):
            scale = 2.0 ** (row - len(data))
    
            imshow(
                array([abs(data[row])]),
                interpolation = 'nearest',
                vmin = vmin,
                vmax = vmax,
                extent = [0, 1, bottom, bottom + scale])
    
            bottom += scale
    
    # Load the signal, take the first channel, limit length to a power of 2 for simplicity.
    rate, signal = wavfile.read('kitten.wav')
    signal = signal[0:lepow2(len(signal)),0]
    tree = pywt.wavedec(signal, 'db5')
    
    # Plotting.
    gray()
    scalogram(tree)
    show()
    

    您可能还希望按级别自适应地缩放值。

    附言-尽管这个问题现在已经很老了,但我想我会在这里回答,因为当我在寻找使用MPL创建标度图的方法时,谷歌上出现了这个页面。

        3
  •  3
  •   robince    16 年前

    matplotlib.image.NonUniformImage 。但这只会有助于实现非均匀轴——我认为你无法像你想的那样自适应地绘制(我认为图像中的每个点总是有相同的面积——所以你必须多次绘制更宽的行)。你有什么理由需要绘制完整的数组吗?显然,所有细节都不会出现在任何图中——所以我建议对原始矩阵进行大量降采样,这样你就可以根据需要复制行来获得图像,而不会耗尽内存。

        4
  •  2
  •   Eric O. Lebigot    16 年前

    如果你想两者都能缩放

    from matplotlib import patches
    axes = subplot(111)
    axes.add_patch(patches.Rectangle((0.2, 0.2), 0.5, 0.5))
    

    PS:在我看来,thrope的响应(matplotlib.image.NounformImage)实际上可以以一种比这里描述的“手动”方法更简单的方式做你想做的事情!