Skip to content

Commit

Permalink
tweaked hist a bit
Browse files Browse the repository at this point in the history
now can enter a "ymax", and the different plots are not locked in terms of their Y scales
  • Loading branch information
trmrsh committed Jun 9, 2021
1 parent 470875a commit 237718b
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions hipercam/scripts/hist.py
Expand Up @@ -29,45 +29,48 @@ def hist(args=None):
Parameters:
input : string
input : str
name of the MCCD file
ccd : string
ccd : str
CCD or CCDs to plot in histogram form, one CCD per panel. Can be '0'
for all of them or a specific set '2 3 5'
window : string
window : str
the window label to show. '0' will merge all windows on each CCD.
x1 : float
x1 : float
lower end of histogram range
x2 : float
x2 : float
upper end of histoigram range. Set == x1 to get automatic min to max.
nbins : int
nbins : int
number of bins to use for the histogram
gplot : bool
ymax : float
maximum y value for plot; 0 for automatic
gplot : bool
plot a gaussian of the same mean and RMS as the data. Note
this is not a fit.
nx : int
nx : int
number of panels across to display, prompted if more than one CCD is
to be plotted.
msub : bool
msub : bool
True/False to subtract median from each window before scaling
nint : bool [if msub]
nint : bool [if msub]
True/False to take the nearest integer of the median or not. Can help
with histograms of digitised data.
device : string [hidden]
device : str [hidden]
Plot device name. Either 'term' for an interactive plot or a
name like 'plot.pdf' for a hard copy.
width : float [hidden]
width : float [hidden]
plot width (inches). Set = 0 to let the program choose.
height : float [hidden]
Expand All @@ -88,6 +91,7 @@ def hist(args=None):
cl.register("x1", Cline.LOCAL, Cline.PROMPT)
cl.register("x2", Cline.LOCAL, Cline.PROMPT)
cl.register("nbins", Cline.LOCAL, Cline.PROMPT)
cl.register("ymax", Cline.LOCAL, Cline.PROMPT)
cl.register("gplot", Cline.LOCAL, Cline.PROMPT)
cl.register("nx", Cline.LOCAL, Cline.PROMPT)
cl.register("msub", Cline.GLOBAL, Cline.PROMPT)
Expand All @@ -97,7 +101,9 @@ def hist(args=None):
cl.register("height", Cline.LOCAL, Cline.HIDE)

# get inputs
frame = cl.get_value("input", "frame to plot", cline.Fname("hcam", hcam.HCAM))
frame = cl.get_value(
"input", "frame to plot", cline.Fname("hcam", hcam.HCAM)
)
mccd = hcam.MCCD.read(frame)

# define the panel grid
Expand All @@ -121,7 +127,12 @@ def hist(args=None):
range = (x1, x2)
else:
range = None
nbins = cl.get_value("nbins", "number of bins for the histogram", 100, 2)
nbins = cl.get_value(
"nbins", "number of bins for the histogram", 100, 2
)
ymax = cl.get_value(
"ymax", "maximum Y value for plots [0 for automatic]", 0., 0.
)
gplot = cl.get_value("gplot", "overplot gaussians?", True)

if len(ccds) > 1:
Expand All @@ -141,25 +152,18 @@ def hist(args=None):
width = cl.get_value("width", "plot width (inches)", 0.0)
height = cl.get_value("height", "plot height (inches)", 0.0)

# OK, inputs obtained, plot
if width > 0 and height > 0:
fig = plt.figure(figsize=(width, height))
else:
fig = plt.figure()

mpl.rcParams["xtick.labelsize"] = hcam.mpl.Params["axis.number.fs"]
mpl.rcParams["ytick.labelsize"] = hcam.mpl.Params["axis.number.fs"]

nccd = len(ccds)
ny = nccd // nx if nccd % nx == 0 else nccd // nx + 1

ax = None
for n, cnam in enumerate(ccds):
if ax is None:
axes = ax = fig.add_subplot(ny, nx, n + 1)
else:
axes = fig.add_subplot(ny, nx, n + 1, sharex=ax, sharey=ax)
if width > 0 and height > 0:
fig,axs = plt.subplots(ny,nx,sharex=True,figsize=(width, height),squeeze=False)
else:
fig,axs = plt.subplots(ny,nx,sharex=True, squeeze=False)

for n, (cnam,ax) in enumerate(zip(ccds,axs.flat)):
ccd = mccd[cnam]

if msub:
Expand All @@ -175,15 +179,15 @@ def hist(args=None):
else:
arr = ccd[wnam].flatten()

n, bins, ps = plt.hist(arr, bins=nbins, range=range)
n, bins, ps = ax.hist(arr, bins=nbins, range=range)

num = len(arr)
mean = arr.mean()
std = arr.std()
if wnam == "0":
plt.title("CCD = {:s}, $\sigma = {:.2f}$".format(cnam, std))
ax.set_title("CCD = {:s}, $\sigma = {:.2f}$".format(cnam, std))
else:
plt.title(
ax.set_title(
"CCD = {:s}, window = {:s}, $\sigma = {:.2f}$".format(cnam, wnam, std)
)

Expand All @@ -198,10 +202,13 @@ def hist(args=None):
/ np.sqrt(2.0 * np.pi)
/ std
)
plt.plot(x, y, "r--", lw=0.5)
ax.plot(x, y, "r--", lw=0.5)

if ymax > 0.:
ax.set_ylim(0,ymax)
ax.set_ylabel("Number of pixels")
ax.set_xlabel("Counts")

plt.ylabel("Number of pixels")
plt.xlabel("Counts")
plt.tight_layout()
if device == "term":
plt.show()
Expand Down

0 comments on commit 237718b

Please sign in to comment.