Imshow:范围和方面 [英] Imshow: extent and aspect

查看:22
本文介绍了Imshow:范围和方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通过 3D 数据集可视化切片和投影的软件系统.我正在使用 matplotlib,特别是 imshow 来可视化我从分析代码中得到的图像缓冲区.

I'm writing a software system that visualizes slices and projections through a 3D dataset. I'm using matplotlib and specifically imshow to visualize the image buffers I get back from my analysis code.

因为我想用绘图轴注释图像,所以我使用 imshow 提供的范围关键字将图像缓冲区像素坐标映射到数据空间坐标系.

Since I'd like to annotate the images with plot axes, I use the extent keyword that imshow supplies to map the image buffer pixel coordinates to a data space coordinate system.

不幸的是,matplotlib 不知道单位.假设(以人为示例)我想绘制尺寸为 1000 m X 1 km 的图像.在这种情况下,范围将类似于 [0, 1000, 0, 1].即使图像数组是正方形,由于extent 关键字隐含的纵横比为1000,因此生成的绘图轴的纵横比也为1000.

Unfortuantely, matplotlib doesn't know about units. Say (taking an artificial example) that I want to plot an image with dimensions of 1000 m X 1 km. In that case the extent would be something like [0, 1000, 0, 1]. Even though the image array is square, since the aspect ratio implied by the extent keyword is 1000, the resulting plot axes also have an aspect ratio of 1000.

是否可以在保持自动生成的主要刻度线和标签的同时强制绘图的纵横比,我通过使用范围关键字获得?

Is it possible to force the aspect ratio of the plot while still keeping the automatically generated major tick marks and labels I get by using the extent keyword?

推荐答案

您可以通过手动设置图像的方面(或让它自动缩放以填充图形的范围)来实现.

You can do it by setting the aspect of the image manually (or by letting it auto-scale to fill up the extent of the figure).

默认情况下,imshow 将绘图的方面设置为 1,因为这通常是人们想要的图像数据.

By default, imshow sets the aspect of the plot to 1, as this is often what people want for image data.

就您而言,您可以执行以下操作:

In your case, you can do something like:

import matplotlib.pyplot as plt
import numpy as np

grid = np.random.random((10,10))

fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(6,10))

ax1.imshow(grid, extent=[0,100,0,1])
ax1.set_title('Default')

ax2.imshow(grid, extent=[0,100,0,1], aspect='auto')
ax2.set_title('Auto-scaled Aspect')

ax3.imshow(grid, extent=[0,100,0,1], aspect=100)
ax3.set_title('Manually Set Aspect')

plt.tight_layout()
plt.show()

这篇关于Imshow:范围和方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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