使图像输入适合绘图中的范围-Python [英] Fits image input to a range in plot - Python

查看:73
本文介绍了使图像输入适合绘图中的范围-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以缩放"要绘制的图像输入到绘图范围内. 更清楚地说,这就是我所需要的:

I'm wondering if I could "scale" a image input to be plotted to a range in the plot. To be more clear, this is what I need:

我有一个基于间隔为-1..1的函数生成的400 * 400图像.因此,我进行了翻译以保存此数据,如下所示:

I have a 400 * 400 image that is generated based on a function which interval is -1..1. So, I do a translate to save this data, like this:

x = Utils.translate(pos_x, 0, self.width, -1, 1)
y = Utils.translate(pos_y, 0, self.height, -1, 1)
data = Utils.map_position_to_function(x, y)

即,首先我将其位置映射到我的范围,然后根据此新位置"计算de f(x,y)并保存数据.

I.e, first I map its position to my range and then I calculate de f(x, y) based on this "new position" and save the data.

问题是,稍后,我必须在功能范围内表示图像轮廓. 因此,我有一张400 * 400的图像,我必须在一个范围为-1..1的图中表示.

The problem is that, later, I have to represent the image contour in the function range. So, I have an image, 400 * 400, that I have to represent in a plot which range is -1..1.

这很好用:

import pylab as plt

im = plt.array(Image.open('Mean.png').convert('L'))
plt.figure()
plt.contour(im, origin='image')
plt.axis('equal')

但是我找不到将y/y轴设置在-1..1范围内的方法

But I couldn't find a way to have the x/y axes in the range -1..1

我尝试过:

row = np.linspace(-1,1,0.25)
X,Y = np.meshgrid(row,row)
Z = plt.array(Image.open('Mean.png').convert('L'))
plt.contour(X,Y,Z)

但是它不起作用,并且不起作用是有道理的,但是我不知道我该怎么做.我在此图像中具有有关数据的信息,因此我也尝试执行以下两种方法:

But it doesn't works, and it makes senses to not work, but I don't know how could I do what I want. I have the information about the data in this image, so I also tried to do something like these two approaches:

# 1
plt.figure()
row = np.linspace(-1,1,0.25)
X,Y = np.meshgrid(row,row)
Z = ImageMedia[Utils.translate(X, 400, 400, -1, 1), Utils.translate(Y, 400, 400, -1, 1)]
plt.contour(X,Y,Z)

# 2
im = plt.array(Image.open('Mean.png').convert('L'))
plt.figure()
plt.contour(im, origin='image')
v = [-1, 1, -1, 1]
plt.axis(v)

哪一个也不起作用.

任何帮助将不胜感激. 谢谢.

Any help would be much appreciated. Thanks.

推荐答案

您可以使用extent kwarg简单地做到这一点:

You can do this simpily using the extent kwarg:

im = ax.imshow(data, ..., extent=[-1, 1, -1, 1])

( doc )它也可以与contour一起使用,contourf等.

(doc) It will also work with contour, contourf ect.

例如:

fig, ax2 = plt.subplots(1, 1)

im = rand(400, 400)
ax2.imshow(im, interpolation='none', extent=[-1, 1, -1, 1])

解决此问题的另一种方法是,如果您真的不想使用extent并让您的生活更轻松,那就是劫持格式化程序以插入缩放因子:

An alternate way to deal with this is, if you really don't want to use extent and make your life easier is to hi-jack the formatter to insert a scaling factor:

from matplotlib.ticker import FuncFormatter

fig, ax2 = plt.subplots(1, 1)

im = rand(400, 400)
ax2.imshow(im, interpolation='none', origin='bottom')

nbins = 5
scale_factor = .5
form_fun = lambda x, i, scale_factor=scale_factor: '{:.3f}'.format(scale_factor * x)
ax2.get_xaxis().set_major_formatter(FuncFormatter(form_fun))
ax2.get_yaxis().set_major_formatter(FuncFormatter(form_fun))
ax2.get_xaxis().get_major_locator().set_params(nbins=nbins)
ax2.get_yaxis().get_major_locator().set_params(nbins=nbins)

这篇关于使图像输入适合绘图中的范围-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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