0

enter image description hereI'm trying to do 4 plots using for loop.But I'm not sure how to do it.how can I display the plots one by one orderly?or save the figure as png? Here is my code:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from astropy.io import fits
import pyregion
import glob
# read in the image
xray_name = glob.glob("*.fits")
for filename in xray_name:
    f_xray = fits.open(filename)
    #name = file_name[:-len('.fits')] 
try:
    from astropy.wcs import WCS
    from astropy.visualization.wcsaxes import WCSAxes
    wcs = WCS(f_xray[0].header)
    fig = plt.figure()
    ax =  plt.subplot(projection=wcs)
    fig.add_axes(ax)
except ImportError:
  ax = plt.subplot(111)
ax.imshow(f_xray[0].data, cmap="summer", vmin=0., vmax=0.00038, origin="lower")
reg_name=glob.glob("*.reg")  
for i in reg_name:
    r =pyregion.open(i).as_imagecoord(header=f_xray[0].header)
    from pyregion.mpl_helper import properties_func_default
    # Use custom function for patch attribute
def fixed_color(shape, saved_attrs):
    attr_list, attr_dict = saved_attrs
    attr_dict["color"] = "red"
    kwargs = properties_func_default(shape, (attr_list, attr_dict))
    return kwargs
# select region shape with tag=="Group 1"
r1 = pyregion.ShapeList([rr for rr in r if rr.attr[1].get("tag") == "Group 1"])
patch_list1, artist_list1 = r1.get_mpl_patches_texts(fixed_color)
r2 = pyregion.ShapeList([rr for rr in r if rr.attr[1].get("tag") != "Group 1"])
patch_list2, artist_list2 = r2.get_mpl_patches_texts()
for p in patch_list1 + patch_list2:
    ax.add_patch(p)
    #for t in artist_list1 + artist_list2:
    #    ax.add_artist(t)
    plt.show()

the aim of the code is to plot a region on fits file image,if there is a way to change the color of the background image to white and the brighter (centeral region) as it is would be okay.Thanks

  • trying to do 4 plots where? Please study existing examples of subplots in mpl documentation, they are very informative. PS. `plt.show` is intended to use only once, not in a loop, for updates `plt.draw` should be used. – ahed87 Nov 30 '17 at 01:14
  • if we think of a plot as a drawing on a piece of paper, or plot-window. If you want one plot per paper, `plt.figure` makes a new paper to draw upon, so somewhere you need 4 plt.figure and associated axis to do the individual plotting on. btw, in your code it's not visible which different data you want to put in the different plots, to me it looks like you only have data to do one plot. – ahed87 Nov 30 '17 at 04:10
  • ahed87,I added the image above I got it right.But the rest three images they only display without two boxes.I feed the the code with ".reg" which which gies those rectangular boxes.plus if there is a way to change the background as white and the centeral(brightest) as black.when you move the "try...except" by 4 space you get 4 images.Thanks – Melaku Sisay Nov 30 '17 at 09:52

1 Answers1

0

You are using colormap "summer" with provided limits. It is not clear to me what you want to achieve since the picture you posted looks more or less digital black and white pixelwise.

In matplotlib there are built in colormaps, and all of those have a reversed twin.

'summer' has a reversed twin with 'summer_r'

This can be picked up in the mpl docs at multiple spots, like colormap example, or SO answers like this.

Hope that is what you are looking for. For the future, when posting code like this, try to remove all non relevant portions as well as at minimum provide a description of the data format/type. Best is to also include a small sample of the data and it's structure. A piece of code only works together with a set of data, so only sharing one is only half the problem formulation.

ahed87
  • 1,240
  • 10
  • 10
  • I agree with sharing the data as well but its to big.regard to the colormap yeah I have changed it to "Greys_r".I'm already figuring the problem,thanks – Melaku Sisay Nov 30 '17 at 20:25