如何使用imshow使用Matplotlib绘制具有非线性y轴的图像? [英] How to plot an image with non-linear y-axis with Matplotlib using imshow?

查看:53
本文介绍了如何使用imshow使用Matplotlib绘制具有非线性y轴的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Matplotlib 将二维数组绘制为图像,且其y比例相对于两个元素的幂y值?

How can I plot an 2D array as an image with Matplotlib having the y scale relative to the power of two of the y value?

例如,数组的第一行在图像中的高度为1,第二行的高度为4,依此类推(单位无关) 用单词解释并不简单,所以请看这张图片(这就是我想要的结果):

For instance the first row of my array will have a height in the image of 1, the second row will have a height of 4, etc. (units are irrelevant) It's not simple to explain with words so look at this image please (that's the kind of result I want):

替代文本http://support.sas.com /rnd/app/da/new/802ce/iml/chap1/images/wavex1k.gif

您会看到第一行比上一行小2倍,依此类推.

As you can see the first row is 2 times smaller that the upper one, and so on.

对于那些对我为什么要这样做感兴趣的人:

For those interested in why I am trying to do this:

我有一个很大的浮点数组(10、700000),代表声音文件的离散小波变换系数.我正在尝试使用这些系数绘制比例尺图. 我可以复制数组x次,直到获得所需的图像行大小,但内存无法容纳太多信息...

I have a pretty big array (10, 700000) of floats, representing the discrete wavelet transform coefficients of a sound file. I am trying to plot the scalogram using those coefficients. I could copy the array x times until I get the desired image row size but the memory cannot hold so much information...

推荐答案

您是否尝试过转换轴?例如:

Have you tried to transform the axis? For example:

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

这意味着对于不存在的坐标,数据中必须存在间隙,除非有一种方法可以提供转换功能,而不仅仅是列表(从未尝试过).

This means there must be gaps in the data for the non-existent coordinates, unless there is a way to provide a transform function instead of just lists (never tried).

修改:

我承认这只是一个线索,而不是一个完整的解决方案.这是我更详细的意思.

I admit it was just a lead, not a complete solution. Here is what I meant in more details.

假设您的数据位于数组a中.您可以使用像这样的转换:

Let's assume you have your data in an array, a. You can use a transform like this one:

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函数转换数组或列表的第一个坐标(您可以根据需要进行转换-它是log2的简化形式,是自制的).优点是,您可以在需要时将其重新用于其他转换,并且也可以轻松地控制它.

Basically it will transform the first coordinate of an array or list with the mylog2 function (that you can transform as you wish - it's home-made as a simplification of log2). The advantage is, you can re-use that for another transform should you need it, and you can easily control it too.

然后将您的数组映射到该数组,该数组不会复制,而是实例中的本地引用:

Then map your array to this one, which doesn't make a copy but a local reference in the instance:

b = arr(a)

现在您可以显示它,例如:

Now you can display it, for example:

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

这是一个示例(带有包含随机值的数组):

Here is a sample (with an array containing random values):

替代文本http://img691.imageshack.us/img691/8883/clipboard01f.png

这篇关于如何使用imshow使用Matplotlib绘制具有非线性y轴的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆