nonlinear scaling image in figure axis matplotlib

2024/10/11 21:21:02

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 calibration. Since the laser lines and the spectrum were collected in the same way they should be correlated in pixel distance. The relationship between pixel number and wavelength is nonlinear. I have fit the pixel number along the x-axis against the wavelength of the laser lines (blue @ 405nm green @ 532nm and red @ 650nm) using a 3rd degree polynomial with high correlation. I want to plot the spectrum by computing the wavelength( nm) directly from the pixel number and display the wavelength beneath the spectrum. Is this possible without overlapping the image on another figure? spectrograph of Laser Lines

import matplotlib.pyplot as plt
from scipy import ndimage
from pylab import *
import numpy as npimport skimageimage= laser_linesprint(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()
Answer

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 pltimage = 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 pltr = 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()
https://en.xdnf.cn/q/118275.html

Related Q&A

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…

Using scipy kmeans for cluster analysis

I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and …

Scrapy and celery `update_state`

I have the following setup (Docker):Celery linked to Flask setup which runs the Scrapy spider Flask setup (obviously) Flask setup gets request for Scrapy -> fire up worker to do some workNow I wish …

SPIDEV on raspberry pi for TI DAC8568 not behaving as expected

I have a Texas Instruments DAC8568 in their BOOST breakout board package. The DAC8568 is an 8 channel, 16bit DAC with SPI interface. The BOOST package has headers to connect it to my raspberry pi, an…

Tensorflow: Simple Linear Regression using CSV data

I am an extreme beginner at tensorflow, and i was tasked to do a simple linear regression using my csv data which contains 2 columns, Height & State of Charge(SoC), where both values are float. In …

How to resolve positional index error in python while solving a condition in python?

I have the following data and I am trying the following code: Name Sensex_index Start_Date End_Date AAA 0.5 20/08/2016 25/09/2016 AAA 0.8 26/08/2016 …

Google Calendar API: Insert multiple events (in Python)

I am using the Google Calendar API, and have successfully managed to insert a single event into an authorized primary calendar, but I would like to hard code multiple events that, when executed, would …

Remove special characters from column headers

I have a dictionary (data_final) of dataframes (health, education, economy,...). The dataframes contain data from one xlsx file. In one of the dataframes (economy), the column names have brackets and s…

Python Flask application getting OPTIONS instead of POST

I have a python Flask listener waiting on port 8080. I expect another process to make a series of POSTs to this port.The code for listener is as follows.#!/usr/bin/env python2 from __future__ import pr…