Imshow-分割为不同的像素大小 [英] Imshow - splitted with different pixel sizes

查看:112
本文介绍了Imshow-分割为不同的像素大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获得以下图片中应说明的内容.

I try to get the following which should be illustrated in the figure below.

为了简单起见,我们假设我有一个numpy数组(10x10),我想使用matplotlib imshow进行绘制.条件是具有不同的像素大小,例如:前五行的大小应为0.5cm,后五行的大小应为1cm.列的大小应相同.

Let us assume, for simplicity, I have a numpy array (10x10) which I want to plot with matplotlib imshow. The condition is to have different pixel sizes, for instance: the first five rows should have a size of 0.5cm the last five rows should have a size of 1cm. The columns should have the same size.

我如何轻松实现这一目标?我已经尝试过这样做,但是我不喜欢这种解决方案.特别是我仍然有白色边框,并且缩放非常糟糕.

How could I easly implement this? I already tried to do this, but I do not like this solution; in particular I still have white borders and zooming is terrible.

from matplotlib import pyplot as pl
import numpy as np

data = np.arange((100))
data = np.reshape(data, (10,10))

figure, (ax1, ax2) = pl.subplots(2, 1, sharex='col')
figure.subplots_adjust(hspace=0)
data1=data[5:10,:]
ax1.imshow(data1, origin="lower", interpolation="none", aspect=0.5, extent=[-0.5,10.5,5.5,10.5], vmax=np.amax(data), vmin=np.amin(data))
ax1.set_ylim([5.5,10.5])
##
data2=data[0:5,:]
ax2.imshow(data2, origin="lower", interpolation="none", aspect=1, extent=[-0.5,10.5,-0.5,5.5], vmax=np.amax(data), vmin=np.amin(data))
ax2.set_ylim([-0.5,5.5])
pl.show()

谢谢

推荐答案

如果您仅使用一个轴对象,则此方法更简单.然后缩放也可以完美地工作.

This is way simpler if you just use a single axes object. Then also zooming will work flawlessly.

代码:

from matplotlib import pyplot as plt
import numpy as np

# prepare the data
data = np.arange((100))
data = np.reshape(data, (10,10))
data1=data[0:5,:]
data2=data[5:10,:]

# create the figure and a single axis
fig, ax = plt.subplots()

# common arguments to imshow
kwargs = dict(
        origin='lower', interpolation='nearest', vmin=np.amin(data),
        vmax=np.amax(data), aspect='auto')

# draw the data
ax.imshow(data1, extent=[0, 10, 0, 5], **kwargs)
ax.imshow(data2, extent=[0, 10, 5, 7.5], **kwargs)

# optional black line between data1 and data2
ax.axhline(5, color='k')

# set the axis limits
ax.set_ylim(0, 7.5)
ax.set_xlim(0, 10)

# set the xticklabels
xticks = np.arange(0,10)
ax.set_xticks(xticks + 0.5)
ax.set_xticklabels(map(str, xticks))

# set the yticks and labels
yticks = np.concatenate((
        np.arange(0, 5) + 0.5,
        np.arange(5, 7.5, 0.5) + 0.25
        ))
ax.set_yticks(yticks)
ax.set_yticklabels(map(str, xticks))

# show the figure
plt.show()

结果:

评论:

  • 我可以自由地以更直观的方式重命名data1/2对象
  • 感谢@kazemakase指出需要调整轴刻度.
  • I took the liberty to rename the data1/2 objects in a more intuitive way
  • Thanks to @kazemakase for pointing out the need to adapt the axis ticks.

这篇关于Imshow-分割为不同的像素大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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