vmin vmax algorithm matplotlib
Asked Answered
H

2

3

I write script for calibration of image (dark frame and flat field)...Here is part of code

for n in range(len(img)):
  with pyfits.open(img[n], mode='update', memmap=True) as im:
    imgg = im[0].data
    header = im[0].header
    imgg.astype(float)
    imgg = (imgg - dd) / df
    imgg[np.isnan(imgg)] = 1
    imgg.astype(int)
    plt.imshow(imgg, cmap=plt.cm.Greys_r, vmin=0.5, vmax=1.5)
    plt.show()

This part of code make calibration of image with dark frame and flat field... When I use at the plotting vmin and vmax, I get the right picture but I don't know how vmin and vmax work. I need to apply this on image data (imgg) because when I save data I get images without vmin and vmax...

Any suggestions?

And the second question... How I can save data changes in fits files? When I used im.close() this work only on one file but don't work in loop.

Thanks

edit

OK here is full script

import numpy as np
import pyfits
from matplotlib import pyplot as plt
import glob


dark=glob.glob('.../ha/dark/*.fits')
flat=glob.glob('.../ha/flat/*.fits')
img=glob.glob('.../ha/*.fits')

sumd0 = pyfits.open(dark[0])
sumdd=sumd0[0].data
sumdd.astype(float)
for i in range(1,len(dark)):
     sumdi=pyfits.open(dark[i])
     sumdi=sumdi[0].data
     sumdd=sumdd.astype(float)+sumdi.astype(float)
dd=sumdd/len(dark)

sumf0 = pyfits.open(flat[0])
sumff=sumf0[0].data
sumff.astype(float)
for i in range(1,len(flat)):
     sumfi=pyfits.open(flat[i])
     sumfi=sumfi[0].data
     sumff=sumff.astype(float)+sumfi.astype(float)

ff=sumff/len(flat)

df=(ff-dd)

for n in range(len(img)):
    with pyfits.open(img[n],mode='update',memmap=True) as im:
        imgg=im[0].data
        header=im[0].header
        imgg.astype(float)
        imgg=(imgg-dd)/df
        imgg.astype(int)
plt.imshow(imgg,cmap=plt.cm.Greys_r,vmin=0.5,vmax=1.5)
plt.show()
Hollins answered 5/7, 2015 at 16:49 Comment(3)
Can you please show your full script including imports?Revel
A few unrelated notes on your code: There are several places where you're calling things like sumdd.astype(float), without assigning the result to a new variable. The .astype() method returns a new array object, so just calling that in-place on an array does nothing. Incidentally there's no reason to keep casting to float. As long as one of the arrays is float all operations will automatically cast to float. for i in range(1,len(dark)):--here you can just iterate directly over the list of filenames, like for filename in dark:. No need to use range. Same in the other two for loops.Stephan
Incidentally, if you're just trying to plot astronomical images you should look into using packages like aplpy instead: aplpy.github.io It's built on matplotlib, but makes details like rescaling and normalizing easy.Stephan
S
7

A bit ofuscated question but I think this does what you want (from your comment in the other answer).

To clamp the data with the same behaviour as vmin and vmax, use np.clip:

np.clip(data, min, max)

In your case:

data = np.clip(data, 0.5, 1.5)
Shadrach answered 5/7, 2015 at 21:2 Comment(4)
np.clip only clips out of bounds values - it doesn't rescale values into the range (min, max).Gallop
yes this is it what i want...thanks ...i had some solution with np.where but this is betterHollins
@Gallop the OP whant to mimic the behaviour of imshow's vmin and vmax, and clip is actually that.Shadrach
@Gallop Yep, it wasn't very clear. I guessed what he wanted by its comment in the other answer.Shadrach
O
5

The use of vmin and vmax arguments in imshow are used in conjunction with norm to normalize your data.

Example:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,10)
y = np.sin(x)

data = np.array([x,y])

# WITHOUT VMIN AND VMAX
im = plt.imshow(data,cmap = plt.get_cmap('jet'))
plt.colorbar(im)
plt.show() 

You get a plot like this, wherein imshow normalizes the data to its min and max.

enter image description here

But when we set vmin and vmax to 0 and 1, the colours will be normalised as if there was a value 0 and a value 1 present in the data.

Here we change imshow as

im = plt.imshow(data,cmap = plt.get_cmap('jet'), vmin=0, vmax=1)

enter image description here

as you can see from the colourbar that it is normalised to 0 and 1.

Outmost answered 5/7, 2015 at 20:14 Comment(4)
I would say clamped rather than normalized. The values >1 are set to 1 in the image, and values <0 to 0. Normalization would refer to reescaling, making the image exactly the same, but with values in [0,1].Shadrach
OK thank...but how I can normalize array for example between 0.5 and 1.5 values? I want normalize only data array without plt.imshow() (vmin vmax)...in my script imggHollins
@imaluengo: well the question wasn't clear in the first place!Outmost
@ThePredator yep I give you that haha. It was just a minor comment that vmin and vmax don't normalize the data, they clamp it. Turned out to be the answer the OP wanted lolShadrach

© 2022 - 2024 — McMap. All rights reserved.