Вопрос

As I present here (in 2D), I'm wondering if how I could "scale" an image input to be plotted to a range in the plot. To be more clear, this is what I need:

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)

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. Also, as I save this image, I do a translate in data so it fits in a 0..255 range, generating a grayscale image.

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.

In 2D, I could do this with this code:

im = plt.array(Image.open('Mean.png').convert('L'))
plt.figure()
CS = plt.contour(im, origin='image', extent=[-1, 1, -1, 1])
plt.clabel(CS, inline=1, fontsize=10)
plt.savefig("CountorLevel2D.png")

2D Contour

In 3D, I tried this:

fig = plt.figure()
ax = fig.gca(projection='3d')
row = np.linspace(0, 400, 400)
X,Y = np.meshgrid(row,row)
CS = ax.contour(X, Y, im, cmap=cm.coolwarm, extent=[-1, 1, -1, 1])
plt.clabel(CS, inline=1, fontsize=10)
plt.savefig("ContourLevel3D.png")

3D Contour

But the X and Y are still in the 0..400 range. I don't know what is wrong, or what I have to do to have X and Y in -1..1 range. Besides, no label is shown.

Also, as im is an image, I read values which ranges between 0..255 values. How could I present this in an -1..1 range too?

Thanks in advance. (:

Это было полезно?

Решение

Try this insted:

fig = plt.figure()
ax = fig.gca(projection='3d')
row = np.linspace(-1, 1, 400)
X,Y = np.meshgrid(row,row)
CS = ax.contour(X, Y, im, cmap=cm.coolwarm)
plt.clabel(CS, inline=1, fontsize=10)
plt.savefig("ContourLevel3D.png")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top