What I have done: I have x,y,v arrays of data that which I bin in 2D xy plane and let v values fall into the bins (or we can call them cells). Then I plot the mean of v values in each cell and make a heatmap of it.
import numpy as np
import matplotlib.pyplot as plt
x=np.array([11,12,12,13,21,14])
y=np.array([28,5,15,16,12,4])
v=np.array([10,5,2,10,6,7])
x = x // 4
y = y // 4
k=10
cells = [[[] for y in range(k)] for x in range(k)] #creating cells or pixels on x-y plane
#letting v values to fall into the grid cells
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
#getting mean from velocity values in each cell
mean_v = [[[] for y in range(k)] for x in range(k)]
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
this = cells[ycell][xcell]
mean_v[ycell][xcell] = np.mean(cells[ycell][xcell])
mean_pix= mean_v[ycell][xcell]
fig, ax = plt.subplots()
mean_v=np.array(mean_v)
mean_masked = np.ma.masked_where(mean_v == 0, mean_v)
plt.imshow(mean_masked, origin='lower')
plt.colorbar()
What I want to do: I want to plot histograms of each of those cells that are not empty on that xy plane. That is, I want the output plot to have the xy axis that is seen in my attached figure, but instead of the mean colour appearing in the cells, I want to see histograms of v-values in those cells. Thanks
This can be achieved using the ax.inset_axes
function with transform=ax.transData
, to add new Axes instances at given locations on your existing Axes. The you can use ax.hist
to plot the histograms on those inset axes.
For example:
import numpy as np
import matplotlib.pyplot as plt
x=np.array([11,12,12,13,21,14])
y=np.array([28,5,15,16,12,4])
v=np.array([10,5,2,10,6,7])
x = x // 4
y = y // 4
k=10
cells = [[[] for y in range(k)] for x in range(k)] #creating cells or pixels on x-y plane
#letting v values to fall into the grid cells
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
#getting mean from velocity values in each cell
mean_v = [[[] for y in range(k)] for x in range(k)]
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
this = cells[ycell][xcell]
mean_v[ycell][xcell] = np.mean(cells[ycell][xcell])
mean_pix= mean_v[ycell][xcell]
fig, ax = plt.subplots()
mean_v=np.array(mean_v)
mean_masked = np.ma.masked_where(mean_v == 0, mean_v)
# Set limits for main Axes
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_aspect('equal')
# Cells to loop over and create histograms for
yy, xx = np.where(mean_v > 0)
# Loop over cells
for i, j in zip(xx, yy):
# Create inset axes. Width and height are 1 since the cells are unit cells
axhist = ax.inset_axes([i, j, 1, 1], transform=ax.transData)
# Plot the histogram. Here we use integer bins
axhist.hist(v[(y==j) & (x==i)], bins=range(11), density=True)
# Turn off ticks and labels, and set the histogram axes limits
axhist.set_xticks([])
axhist.set_yticks([])
axhist.set_xlim(0, 10)
axhist.set_ylim(0, 1)
plt.show()
As you can see in your toy dataset, there are only one or two entries of v
per cell for now, so the histograms aren't that exciting, but this should work for a larger dataset too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.