Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib imshow -- use any vector as axis

I would like to use any vector as an axis in plt.imshow().

A = np.random.rand(4, 4)
x = np.array([1, 2, 3, 8])
y = np.array([-1, 0, 2, 3])

I imagine something like this:

plt.imshow(a, x_ax=x, y_ax=y)

I know there is an extent parameter available, but sadly it does not allow for non-equally spaced vectors.

Can anyone please help? Thanks in advance.

like image 385
petervanya Avatar asked Jul 24 '17 16:07

petervanya


People also ask

Does Imshow normalize image?

By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

What is the difference between PLT Imshow and PLT show?

imshow() draws an image on the current figure (creating a figure if there isn't a current figure). Calling plt. show() before you've drawn anything doesn't make any sense. If you want to explictly create a new figure, use plt.

What is CMAP in PLT Imshow?

cmap : ~matplotlib.colors.Colormap , optional, default: None. If None, default to rc image.cmap value. cmap is ignored when X has RGB(A) information. However, if img were an array of shape (M,N) , then the cmap controls the colormap used to display the values.

What is interpolation in Imshow?

This example displays the difference between interpolation methods for imshow . If interpolation is None, it defaults to the rcParams["image. interpolation"] (default: 'antialiased' ). If the interpolation is 'none' , then no interpolation is performed for the Agg, ps and pdf backends.


1 Answers

Imshow plots are always equally spaced. The question would be if you want to have
(a) an equally spaced plot with unequally spaced labels, or
(b) an unequally spaced plot with labels to scale.

(a) equally spaced plot

import numpy as np
import matplotlib.pyplot as plt

a = np.random.rand(4, 4)
x = np.array([1, 2, 3, 8])
y = np.array([-1, 0, 2, 3])

plt.imshow(a)
plt.xticks(range(len(x)), x)
plt.yticks(range(len(y)), y)
plt.show()

enter image description here

(b) unequally spaced plot

import numpy as np
import matplotlib.pyplot as plt

a = np.random.rand(3, 3)
x = np.array([1, 2, 3, 8])
y = np.array([-1, 0, 2, 3])
X,Y = np.meshgrid(x,y)

plt.pcolormesh(X,Y,a)

plt.xticks(x)
plt.yticks(y)
plt.show()

enter image description here

Note that in this case the "vector" would specify the edges of the grid, thus they would only allow for a 3x3 array.

like image 147
ImportanceOfBeingErnest Avatar answered Oct 19 '22 23:10

ImportanceOfBeingErnest