0

I've written a Python library that uses Matplotlib and Astropy to generate spectra from data of solar radio emissions. I'm satisfied with how it's plotting data from a single FITS file, but now I'm trying to plot data from multiple FITS files in a single figure adjacently. I've read some of Matplotlib's documentation and some questions related to this like How do I get multiple subplots in matplotlib?.

Here's part of the code that plots data from a single FITS file:

def plot_freq_range_db_above_background(self, start_freq, end_freq):
        plt.figure(1, figsize=(11, 6))
        plt.imshow(self.hdul_dataset['db'] - self.hdul_dataset['db_median'],
                   cmap='magma', norm=plt.Normalize(self.hdul_dataset['v_min'],
                                                    self.hdul_dataset['v_max']
                                                    ),
                   aspect='auto', extent=[self.hdul_dataset['time_axis'][0],
                                          self.hdul_dataset['time_axis']
                                          [-1000],
                                          self.hdul_dataset['frequency'][-1],
                                          self.hdul_dataset['frequency'][0]])
        plt.ylim(start_freq, end)
        plt.gca().invert_yaxis()
        plt.colorbar(label='dB above background')
        plt.xlabel('Time (UT)', fontsize=15)
        plt.ylabel('Frequency (MHz)', fontsize=15)
        plt.title(self.filename, fontsize=16)
        plt.tick_params(labelsize=14)
        plt.show()

And this is an example of a plot generated by the method above:
BLEN7M_20120812_140000_25

So, what I'm trying to do now is to plot data from different files, and have all of them adjacent to each other in a single figure. The X-axis(frequency) is the same for every single plot, and the Y-axis(time) is continuous from one file to the next one.

Here's the method I've written trying to accomplish what I just described:

    def plot_fits_files_list(files_list, start_freq, end_freq):
        dim = len(files_list)
        plt_index = 1
        plt.figure(1)
        for file in files_list:
            fits_filename = file.split(os.sep)[-1]
            fitsfile = ECallistoFitsFile(fits_filename)
            fitsfile.set_file_path()
            fitsfile.set_hdul_dataset()
            plt.subplot(dim, dim, plt_index)
            plt_index += 1
            plt.imshow(
                fitsfile.hdul_dataset['db'] -
                fitsfile.hdul_dataset['db_median'],
                cmap='magma',
                norm=plt.Normalize(fitsfile.hdul_dataset['v_min'],
                                   fitsfile.hdul_dataset['v_max']),
                aspect='auto', extent=[fitsfile.hdul_dataset['time_axis'][0],
                                       fitsfile.hdul_dataset['time_axis']
                                       [-1000],
                                       fitsfile.hdul_dataset['frequency'][-1],
                                       fitsfile.hdul_dataset['frequency'][0]])
            plt.ylim(start_freq, end_freq)
            plt.gca().invert_yaxis()
        plt.colorbar(label='dB above background')
        plt.xlabel('Time (UT)', fontsize=15)
        plt.ylabel('Frequency (MHz)', fontsize=15)
        plt.title("Multiple Plots Test", fontsize=16)
        plt.tick_params(labelsize=14)
        plt.show()

And here's the plot it's generating at the moment: extended_plot.png

  • 1
    can you clarify your problem a little? you want adjacent plots that have the same x axis ticks, but different y axes. Is your issue mostly that they just are overlapping each other?- If this is the case you can add at the end plt.tight_layout() to space them out in that regard. if you want the title to be above all of them add it at the end with fig.suptitle("something"). – djakubosky Mar 21 '19 at 22:15
  • @djakubosky They have the same y axes, they all go from frequencyA to frequencyB (e.g., from 100 MHz to 350MHz). And their x axes are different beacause each plot is from a different time frame, but each plot's time frame is the continuation of the previous one's. For example, the first plot's x-axis goes from 14:00 to 14:15, and the second plot's x-axis goes from 14:15 to 14:30. – Andre Korol Mar 22 '19 at 00:56
  • so is your goal instead to have all of the plots with no space between them? so they can sortof share a y axis from the far left? – djakubosky Mar 22 '19 at 19:02

0 Answers0