Skip to content

Instantly share code, notes, and snippets.

@dantrim
Last active June 22, 2021 23:15
Show Gist options
  • Save dantrim/55a07b22258544ed8e48fc5372c149d7 to your computer and use it in GitHub Desktop.
Save dantrim/55a07b22258544ed8e48fc5372c149d7 to your computer and use it in GitHub Desktop.
Plot a YARR 2D histogram using numpy+matplotlib
#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import griddata
import json
##
## YARR histogram data output file
##
filename = "Q12_Chip1_OccupancyMap.json"
##
## Some useful constants
##
N_COL = 400
N_ROW = 384
##
## first setup the plot and axes
##
fig, ax = plt.subplots(1,1)
ax.tick_params(direction = "in", which = "both",
top = True, bottom = True, left = True, right = True)
ax.grid(True, zorder = 1e9, alpha = 0.05)
# put a tick mark at core boundaries, with no tick labels
xticks = np.arange(0, N_COL, 8)
yticks = np.arange(0, N_ROW, 8)
ax.set_xticks(xticks)
ax.set_yticks(yticks)
ax.set_xticklabels([])
ax.set_yticklabels([])
# labels
ax.set_title("Occupancy Map")
ax.set_xlabel("Column")
ax.set_ylabel("Row")
##
## get the data from the input JSON file of the occupancy map histogram
##
with open(filename, "r") as infile :
data = np.array(json.load(infile)["Data"])
##
## create the 2D plot -- using pcolormesh
##
# create the (x,y) pairs to associate with each occupancy (Z) value
x_data = np.arange(0, N_COL, 1)
y_data = np.arange(0, N_ROW, 1)
x_data, y_data = np.meshgrid(x_data, y_data)
z_data = griddata((x_data.T.flatten(), y_data.T.flatten()), data.flatten(),
(x_data, y_data), method = "nearest")
p = ax.pcolormesh(x_data, y_data, z_data, cmap = "YlGnBu", shading = "auto")
colorbar = fig.colorbar(p, ax = ax)
colorbar.set_label("Occupancy")
fig.show()
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment