MUFC_Rules
MUFC_Rules

Reputation: 35

Displaying a fits image with multiple channels

Here is my expected result

I downloaded a fits image with multiple channels from CASA. I tried to upload the image like I would a normal fits image, but it shows an error

"Invalid dimensions for image data".

The image has a shape (1, 20, 250, 250).

Is there a way to display all the channels?

When I try the code I did below, it shows only one of the channels.

file2 = "Downloads/PVDiagramtest2.fits"
image_data = fits.getdata(file2)

image_data = image_data[~np.isnan(image_data)]

plt.figure()
plt.imshow(image_data[0,0,:,:])

plt.show()

Upvotes: 1

Views: 1927

Answers (1)

user11563547
user11563547

Reputation:

To see all the images, you can loop through with subplots. A toy example of how to plot multiple images together would look something like this:

import numpy as np
import matplotlib.pyplot as plt   

x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)

for i in range(16):
    plt.subplot(4, 4, i+1)
    plt.scatter(x, y, s=80, c=z, marker=verts)
plt.show() 

In your case, I think that may look something like this:

from astropy.io import fits
# import numpy as np
import matplotlib.pyplot as plt   

file = "WFPC2u5780205r_c0fx.fits"


image_data = fits.getdata(file)

# image_data = image_data[~np.isnan(image_data)]


num_channels = 4
x_dim = 2
y_dim = 2
colors = ['rainbow', 'PuRd_r', 'gist_earth', 'coolwarm']
for i in range(num_channels):
    plt.subplot(x_dim, y_dim, i+1)
    plt.imshow(image_data[i,:,:], cmap=colors[i])
plt.show()

Of note, I commented out where you where bit-flipping on np.isnan() because it seems to be flattening the image array, and I don't see a rational for using this type of approach when it only seems to be introducing an issue. However, perhaps with your data, it behaves as you like.

For this example, I used the first sample FITS file available from the FITS Support Office. The image is 4 channel 200x200 pixels. Other than making the 2x2 grid using subplots, I haven't formatted the image. Here's the output image from this sample code:enter image description here

Upvotes: 1

Related Questions