Making averaged line plot.
Dear all, I have a line plot of some parameter along an axis as a function of distance. Now I would like to get another line plot of the same parameter but averaged over a certain distance (say 100 kpc, the plot below is supposed to be flat after this). I've been searching the document for some time but haven't found a method. Is there a simple way to do this? I would very much appreciate it if anyone could help me on this. =================================== import ... def _ScaledBMag(field, data): return ... .... pf = load(filename) add_field("ScaledBMag", function=_ScaledBMag) c = pf.h.find_max('ScaledBMag')[1] ax = 0 ray = pf.h.ortho_ray(ax, (c[1], c[2])) P.subplot(211) P.semilogy(ray['x'], ray['ScaledBMag']) P.xlabel('x') P.ylabel('ScaledBMag') print "works" P.savefig("ScaledBMag_lineplot.png") [image: Inline image 1] =================================== I'm using yt Version = 2.6.1 Changeset = c994959ed3be =================================== Regards, Dai
Hi Yuxiao, If you would like to start from your current code, the most straightforward way is to write a loop to smooth over a certain distance by each step, which also works for AMR data: smooth_len = 100 # smoothing length of 100 kpc SmoothedBMag = np.copy(ray[’ScaledBMag’]) # store the smoothed array bin_num = np.ceiling((ray[‘x’].max() - ray[‘x’].min()) / smooth_len) # make bins every 100 kpc for step in range(bin_num): mask1 = (ray[‘x’] >= ray[‘x’].min() + step * smooth_len) mask2 = (ray[‘x’] < ray[‘x’].min() + (step + 1) * smooth_len) mask = np.logical_and(mask1, mask2) # mask the cells within a certain length of 100 kpc SmoothedBMag[mask] = np.mean(ray[’ScaledBMag’][mask]) # take the average and save After that the array “SmoothedBMag” is the smoothed one. However, an easier way is to use the the 1D ProfilePlot function (http://yt-project.org/docs/2.6/visualizing/plots.html#d-profile-plots http://yt-project.org/docs/2.6/visualizing/plots.html#d-profile-plots), and in this case you could do something like: plot = ProfilePlot(pf.h.all_data(), ‘x’, [’ScaledBMag’], n_bins=bin_num) Note that this will do the average over entire y-z plane for each x bins, which is different from averaging only an array of a ray object (so maybe it’s not what you want). If you really want the data within a thin slit only, you could define a region object pf.h.region() and do ProfilePlot. I think it’s also doable to use the function “load_uniform_grid” to create a 1D dataset from the arrays in ray object and pass it to ProfilePlot. Best wishes, -- Suoqing JI Ph.D Candidate Department of Physics University of California, Santa Barbara http://web.physics.ucsb.edu/~suoqing
On Mar 14, 2016, at 10:57 PM, Yuxiao Dai
wrote: Dear all,
I have a line plot of some parameter along an axis as a function of distance. Now I would like to get another line plot of the same parameter but averaged over a certain distance (say 100 kpc, the plot below is supposed to be flat after this). I've been searching the document for some time but haven't found a method. Is there a simple way to do this?
I would very much appreciate it if anyone could help me on this.
=================================== import ...
def _ScaledBMag(field, data):
return ...
....
pf = load(filename)
add_field("ScaledBMag", function=_ScaledBMag)
c = pf.h.find_max('ScaledBMag')[1]
ax = 0
ray = pf.h.ortho_ray(ax, (c[1], c[2]))
P.subplot(211)
P.semilogy(ray['x'], ray['ScaledBMag'])
P.xlabel('x')
P.ylabel('ScaledBMag')
print "works"
P.savefig("ScaledBMag_lineplot.png")
=================================== I'm using yt
Version = 2.6.1
Changeset = c994959ed3be
===================================
Regards,
Dai
_______________________________________________ yt-users mailing list yt-users@lists.spacepope.org http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
participants (2)
-
Suoqing Ji
-
Yuxiao Dai