Skip to content Skip to sidebar Skip to footer

Nonlinear Scaling Image In Figure Axis Matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I don't think so. I have an image of a spectrum. I have several laser lines for calibratio

Solution 1:

Probably the easiest option is to use a pcolormesh instead of an imshow plot. The pcolormesh shows the edges of a grid, such that you might simply transform the original grid using the functional dependence between pixels and wavelength to define the edges of each pixel in terms of wavelength.

import numpy as np 
import matplotlib.pyplot as plt

image = np.sort(np.random.randint(0,256,size=(400,600)),axis=0)


f = lambda i: i**3*-3.119E-6+2.926E-3*i**2+0.173*i+269.593
xi = np.arange(0,image.shape[1]+1)-0.5
yi = np.arange(0,image.shape[0]+1)-0.5
Xi, Yi = np.meshgrid(xi, yi)

Xw = f(Xi)

fig, (ax) = plt.subplots(figsize=(8,4))

ax.pcolormesh(Xw, Yi, image)

ax.set_xlabel("wavelength [nm]")
plt.show()

enter image description here

If the image has 3 colorchannels, you need to use the color argument of pcolormesh to set the color of each pixel, as shown in this question: Plotting an irregularly-spaced RGB image in Python

import numpy as np 
import matplotlib.pyplot as plt

r = np.sort(np.random.randint(0,256,size=(200,600)),axis=1)
g = np.sort(np.random.randint(0,256,size=(200,600)),axis=0)
b = np.sort(np.random.randint(0,256,size=(200,600)),axis=1)
image = np.dstack([r, g, b])

color = image.reshape((image.shape[0]*image.shape[1],image.shape[2]))
if color.max() > 1.:
    color = color/255.

f = lambda i: i**3*-3.119E-6+2.926E-3*i**2+0.173*i+269.593
xi = np.arange(0,image.shape[1]+1)-0.5
yi = np.arange(0,image.shape[0]+1)-0.5
Xi, Yi = np.meshgrid(xi, yi)

Xw = f(Xi)

fig, (ax) = plt.subplots(figsize=(8,4))

pc = ax.pcolormesh(Xw, Yi, Xw, color=color )
pc.set_array(None)
ax.set_xlabel("wavelength [nm]")
plt.show()

Post a Comment for "Nonlinear Scaling Image In Figure Axis Matplotlib"