new-to-coding
new-to-coding

Reputation: 51

How can I plot only particular values in xarray?

I am using data from cdasws to plot dynamic spectra. I am following the example found here https://cdaweb.gsfc.nasa.gov/WebServices/REST/jupyter/CdasWsExample.html

This is my code which I have modified to obtain a dynamic spectra for STEREO.

from cdasws import CdasWs
from cdasws.datarepresentation import DataRepresentation
import matplotlib.pyplot as plt
cdas = CdasWs()
import numpy as np

datasets = cdas.get_datasets(observatoryGroup='STEREO')

for index, dataset in enumerate(datasets):
    print(dataset['Id'], dataset['Label'])

variables = cdas.get_variables('STEREO_LEVEL2_SWAVES')
for variable_1 in variables:
    print(variable_1['Name'], variable_1['LongDescription'])

data = cdas.get_data('STEREO_LEVEL2_SWAVES', ['avg_intens_ahead'],
                     '2020-07-11T02:00:00Z', '2020-07-11T03:00:00Z',
                     dataRepresentation = DataRepresentation.XARRAY)[1]
print(data)

plt.figure(figsize = (15,7))
# plt.ylim(100,1000)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.yscale('log')
sorted_data.transpose().plot()
plt.xlabel("Time",size=18)
plt.ylabel("Frequency (kHz)",size=18)
plt.show()

Using this code gives a plot that looks something like this,

Dynamic spectrum

My question is, is there anyway of plotting this spectrum only for a particular frequency? For example, I want to plot just the intensity values at 636 kHz, is there any way I can do that?

Any help is greatly appreciated, I dont understand xarray, I have never worked with it before.

Edit -

Using the command,

data_stereo.avg_intens_ahead.loc[:,625].plot()

generates a plot that looks like, enter image description here

While this is useful, what I needed is;

for the dynamic spectrum, if i choose a particular frequency like 600khz, can it display something like this (i have just added white boxes to clarify what i mean) -

enter image description here

Upvotes: 0

Views: 180

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15432

If you still want the plot to be 2D, but to include a subset of your data along one of the dimensions, you can provide an array of indices or a slice object. For example:

data_stereo.avg_intens_ahead.sel(
    frequency=[625]
).plot()

Or

# include a 10% band on either side
data_stereo.avg_intens_ahead.sel(
    frequency=slice(625*0.9, 625*1.1)
).plot()

Alternatively, if you would actually like your plot to show white space outside this selected area, you could mask your data with where:

data_stereo.avg_intens_ahead.where(
    data_stereo.frequency==625
).plot()

Upvotes: 1

Related Questions