spedas / pyspedas

Python-based Space Physics Environment Data Analysis Software
https://pyspedas.readthedocs.io/
MIT License
143 stars 58 forks source link

Trying to plot ‘psp_spi_EFLUX_VS_ENERGY’ using pyplot.options() and tplot() for SWEAP/SPAN-I instrument. #810

Closed pspathare closed 3 months ago

pspathare commented 3 months ago

I am a beginner in using PySPEDAS. I mainly work with PSP data.

I am trying to plot ‘psp_spi_EFLUX_VS_ENERGY’ using pyplot.options() and tplot() for SWEAP/SPAN-I instrument. Unfortunately, I am not getting the plot I should supposedly get. I am getting the required plot using imshow() function but not using tplot(). I have attached the codes I am writing for both cases. I have also attached the plot image obtained using imshow().

I would appreciate it if you could point out where I am going wrong with tplot().


Using options() and tplot().

import pyspedas import pytplot import numpy as np import matplotlib.pyplot as plt from pytplot import tplot

spi_vars = pyspedas.psp.spi(trange=['2022-12-12/00:00', '2022-12-12/23:59'], datatype='sf00_l3_mom', level='l3', time_clip=True)

time=pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].coords['time'].values print(time)

energy_channel = pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].coords['spec_bins'].values print(energy_channel) ec = energy_channel[0,:]

energy_flux = pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].values print(energy_flux)

e_flux = pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].coords['v'].values

energy_flux[energy_flux == 0]=np.nan

pytplot.store_data('E_Flux',data = {'x':time.T,'y':energy_flux,'v':energy_channel})

pytplot.options('E_Flux', opt_dict={'Spec':1,'zlog':1,'Colormap':'plasma', 'ylog':1}) tplot('E_Flux')



Using imshow()

import pyspedas import pytplot import numpy as np import matplotlib.pyplot as plt from pytplot import tplot import matplotlib.cm as cm from matplotlib.colors import LogNorm from matplotlib.gridspec import GridSpec

spi_vars = pyspedas.psp.spi(trange=['2022-12-12/05:30', '2022-12-12/10:59'], datatype='sf00_l3_mom', level='l3', time_clip=True)

time=pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].coords['time'].values print(time)

Energy_channels = pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].coords['spec_bins'].values

<---- Already in log scale common ratio = 1.2somethings

print(Energy_channels) ec = Energy_channels[0,:] Energy_flux= pytplot.data_quants['psp_spi_EFLUX_VS_ENERGY'].values print(Energy_flux)

Energy_flux[Energy_flux==0]= np.nan

fig = plt.figure(figsize=(12,2.5)) # Created the canvas to create plots and subplots on it gs = fig.add_gridspec(1,1) # this line of code adds a grid to the figure fig with one subplot. ax0 = fig.add_subplot(gs[0,0])# Adding a subplot at the only grid position (0,0) ax0.set_yscale('log') ax0.set_ylim(np.min(ec),np.max(ec)) ax1 = ax0.twinx() #This effectively creates a new subplot within the same grid position

as ax0,

                            # effectively overlaying ax1 on top of ax0 in the same position.
                   #creating a new Axes object (ax1) that shares the same x-axis as ax0 but 
                   #has its own independent y-axis. This 

ax1.axis('off')

img = ax1.imshow(Energy_flux.T, extent=(np.amin(time), np.amax(time), np.amin(Energy_channels), np.amax(Energy_channels)), cmap='jet', norm=LogNorm(), aspect='auto', interpolation= 'none', resample=False, filternorm= False)

creates an image (imshow) of a two-dimensional array (Energy_flux) on the Axes #object ax1.

ax1.set_ylabel('PAD_Eflux') plt.colorbar(img, ax=ax1, label='PAD_Eflux',fraction=0.05,pad=0.025) # adds the

colorbar to the img on axes object ax1

plt.subplots_adjust(left=0.125,right=0.925)


PSP_SPI_Energy_vs_Eflux_using_imshow

jameswilburlewis commented 3 months ago

Are you using the most recent release of pytplot-mpl-temp? (version 2.2.8, released March 27). You can upgrade that package in your pyspedas environment with the following command in a terminal window:

pip install --upgrade pytplot-mpl-temp

That release includes a number of bug fixes for spectrogram plotting. I tried your first example (changing the colormap to 'jet' to match the second example), and when I zoom in to the time interval on the second plot, they look pretty much the same. Here's what I'm seeing:

Screenshot 2024-04-02 at 11 03 58 AM