Consider I have load a dataset as follows:
ds = yt.load('pltxxx')
The dataset includes the following fields density, mag_vort, tracer, x_velocity, y_velocity
One can simply plot the mag_vort which is the magnitude of vorticity in 2D domain in this case, by means of:
slc = yt.SlicePlot(ds, 'z', 'mag_vort')
If I want to export the x-cooridnates, y-coordinates and vorticity_magnitude in the txt file (or numpy array) or plot it via matplotlib scatter plot
plt.scatter(x_coor, y_coor, c=mag_vort)
Is there an easy way to extract those information from dataset?
You can use a data object (in this case we use the all_data
data object) to access the field values for the 'x'
, 'y'
, and 'mag_vort'
fields:
ad = ds.all_data()
x = ad['x']
y = ad['y']
mag_vort = ad['mag_vort']
The arrays you get back from accessing a data object are YTArray
instances. YTArray
is a subclass of numpy's ndarray
that has units attached.
Before you pass these arrays to matplotlib, convert them to whichever units you want to do the plot in, then cast them to numpy arrays:
x_plot = np.array(x.to('km'))
y_plot = np.array(y.to('km'))
plt.scatter(x_plot, y_plot, c=np.array(mag_vort))