Non-linear scaling image on matplotlib axis axis

enter a description of the image here I hope I did not look at each other like the previously asked question. I do not think so. I have a spectrum image. I have several laser lines for calibration. Since the laser lines and spectrum were collected in the same way, they must be correlated at a distance in pixels. The relationship between the number of pixels and the wavelength is nonlinear. I placed the pixel number along the x axis relative to the wavelength of the laser lines (blue @ 405 nm green @ 532 nm and red @ 650 nm) using a highly correlated 3rd degree polynomial. I want to build a spectrum by calculating the wavelength (nm) directly from the number of pixels and displaying the wavelength under the spectrum. Is this possible without overlapping the image in another figure? laser line spectrograph

import matplotlib.pyplot as plt from scipy import ndimage from pylab import * import numpy as np import skimage image= laser_lines print(image.shape) for i in range(image.shape[1]): x=i^3*-3.119E-6+2.926E-3*i^2+0.173*i+269.593 for j in range(image.shape[0]): y=image[i,j] imshow(image) plt.show() 
0
source share
1 answer

Probably the easiest option is to use pcolormesh instead of the imshow graph. pcolormesh shows the edges of the grid, so you can simply transform the original grid using the functional relationship between pixels and wavelength to determine the edges of each pixel by 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 color channels, you need to use the color argument for pcolormesh to specify the color of each pixel, as shown in this question: Overlapping 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() 
+1
source

Source: https://habr.com/ru/post/1263647/


All Articles