How to plot multiple fits images side by side in galactic coordinates using python

How to superimpose fits images using python well in WCS space

Environment

OK if matplotlib and astropy are installed in python. If anconda3 system is included, it is OK by default.

An example of overlapping two images with Galactic coordinates

Taking the visible light and X-ray NGC 4051 images as examples, the plot of the whole image and the plot of the enlarged image near the center are shown.

point

--Just generate a WCS class from the image fits file and put it in the matplotlib projection option. However, with this alone, it is displayed only in the initial image size.

--If you want to enlarge a certain place, cut the WCS object in the same way as the image file. The part of the sample code, hriwcs [hricx-hridx: hricx + hridx, hricy-hridx: hricy + hridx]. Give this to matplotlib's wcs option.

――If you want to display two images on top of each other, it becomes a difficult problem. In that case, it is briefly summarized in How to merge images with WCS coordinates and plot with python.

Code and file storage

For how to take an image, How to search using python's astroquery and get fits image with skyviewを参考にされたい。

Sample code

qiita_plot_fits_2image.py



import matplotlib 
matplotlib.use('TkAgg')
import glob 
import matplotlib.pyplot as plt
import sys
from matplotlib.colors import LogNorm
from astropy.wcs import WCS
from astropy.io import fits

name="NGC4051"

hri = glob.glob(name + '*HRI*.fits')
dss = glob.glob(name + '*DSS*.fits')	

if len(hri) == 1 and len(dss) == 1:
	pass
else:
	print("need to store HRI and DSS fits")

F = plt.figure(figsize=(10,8))

hrifilename = hri[0]
hriname = hrifilename.replace(".fits",".png ")
hrihdu = fits.open(hrifilename)[0]
hriwcs = WCS(hrihdu.header)
hridata = hrihdu.data

hrixlen, hriylen = hridata.shape
hricx = int(0.5 * hrixlen)
hricy = int(0.5 * hriylen)
hridx = int(hrixlen*0.1)
hriwcscut = hriwcs[hricx-hridx:hricx+hridx,hricy-hridx:hricy+hridx]

dssfilename = dss[0]
dssname = dssfilename.replace(".fits",".png ")
dsshdu = fits.open(dssfilename)[0]
dsswcs = WCS(dsshdu.header)
dssdata = dsshdu.data

dssxlen, dssylen = dssdata.shape
dsscx = int(0.5 * dssxlen)
dsscy = int(0.5 * dssylen)
dssdx = int(dssxlen*0.1)
dsswcscut = dsswcs[dsscx-dssdx:dsscx+dssdx,dsscy-dssdx:dsscy+dssdx]

plt.figtext(0.45,0.93, name, size="large")
plt.figtext(0.15,0.9, "X-ray, Rosat HRI")
plt.figtext(0.57,0.9, "Optical, UK Shimidt")				

plt.subplot(221, projection=dsswcs)
#		plt.subplot(221, projection=hriwcs)		
try:
	plt.imshow(hridata, origin='lower', norm=LogNorm())
	plt.colorbar()					
except:
	print("ERROR, couldn't plot log-z scale")			
	plt.close()

plt.grid(color='white', ls='solid')
plt.xlabel('Galactic Longitude')
plt.ylabel('Galactic Latitude')

plt.subplot(223, projection=dsswcscut)
#		plt.subplot(223, projection=hriwcscut)		
plt.imshow(hridata[hricx-hridx:hricx+hridx,hricy-hridx:hricy+hridx], origin='lower')
plt.colorbar()		
plt.grid(color='white', ls='solid')
plt.xlabel('Galactic Longitude')
plt.ylabel('Galactic Latitude')


plt.subplot(222, projection=dsswcs)
try:
	plt.imshow(dssdata, origin='lower', norm=LogNorm())
	plt.colorbar()								
except: 
	print("ERROR, couldn't plot log-z scale")
	plt.imshow(dssdata, origin='lower')
	plt.colorbar()		
	
plt.grid(color='white', ls='solid')
plt.xlabel('Galactic Longitude')

plt.subplot(224, projection=dsswcscut)
plt.imshow(dssdata[dsscx-dssdx:dsscx+dssdx,dsscy-dssdx:dsscy+dssdx], origin='lower')
plt.colorbar()		
plt.grid(color='white', ls='solid')
plt.xlabel('Galactic Longitude')

plt.savefig(name  + ".png ")
plt.close()


Output result

The top two overviews show the z-axis in log, and the bottom two show in linear. When displaying the z-axis in log, the data can only be empty or zero, so try except is used to avoid it.

NGC4051.png

Reference example

[How to search using python's astroquery and get fits images with skyview]( An example of a gif animation created by acquiring X-ray (ROSAT) and visible light (DSS) images using https://qiita.com/yamadasuzaku/items/df185d151248578e8e1e). To make a gif animation, I generated it with convert -delay 100 -loop 1 * .png xrayopt.gif.

--X-ray and visible light (~ 200 celestial bodies): http://www-x.phys.se.tmu.ac.jp/~syamada/qiita/qiita_wcs/xrayopt.gif --Visible light (~ 400 celestial bodies): http://www-x.phys.se.tmu.ac.jp/~syamada/qiita/qiita_wcs/opt.gif

Recommended Posts

How to plot multiple fits images side by side in galactic coordinates using python
How to retrieve multiple arrays using slice in python.
How to collect images in Python
How to plot galaxy visible light data using OpenNGC database in python
[Google Colab] I want to display multiple images side by side in tiles
[python] How to display list elements side by side
How to plot autocorrelation and partial autocorrelation in python
How to take multiple arguments when doing parallel processing using multiprocessing in python
How to write string concatenation in multiple lines in Python
How to display multiple images of galaxies in tiles
How to exit when using Python in Terminal (Mac)
How to execute a command using subprocess in Python
How to develop in Python
How to slice a block multiple array from a multiple array in Python
How to define multiple variables in a python for statement
How to install python using anaconda
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
Object extraction in images by pattern matching using OpenCV with Python
How to delete multiple specified positions (indexes) in a Python list
How to display legend marks in one with Python 2D plot
How to generate a new loggroup in CloudWatch using python within Lambda
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
Send email to multiple recipients in Python (Python 3)
How to work with BigQuery in Python
Log in to Slack using requests in Python
How to get a value from a parameter store in lambda (using python)
Function to save images by date [python3]
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to write a string when there are multiple lines in python
How to dynamically zero pad in Python
How to use regular expressions in Python
[Rails] How to detect radical images by analyzing images using Cloud Vision API
How to display Hello world in python
How to search using python's astroquery and get fits images with skyview
Blender 2.9, Python, Select multiple meshes by coordinates
How to use is and == in Python
How to write Ruby to_s in Python
How to view images in Django's Admin
How to draw OpenCV images in Pygame
How to sort by specifying a column in the Python Numpy array.
How to deal with old Python versions in Cloud9 made by others
How to build an environment for using multiple versions of Python on Mac
How to read all the classes contained in * .py in the directory specified by Python
How to study Python 3 engineer certification basic exam by Python beginner (passed in August 2020)
I made a module in C language to filter images loaded by Python
How to use the C library in Python