Taking average of squares in matplotlib

I currently have a scatter plot with 3 datasets, an x ​​coordinate, a y coordinate, and a value at each x, y. These sets are 1D numpy arrays.

A function matplotlib.axes.Axes.hexbin

in matplotlib accumulates all assigned values ​​at each x, y inside each bin and then takes the average. This creates a color plan with hexagonal bins.

Is it possible to do something like this but with square cells using matplotlib or numpy?

This is the current hexbin code:

plt.hexbin(daylim,Llim, C = elim, gridsize = 168,bins = 'log')

+3


source to share


1 answer


You can calculate the values ​​to be displayed on a 2D plot before printing and then display as a plot imshow

.

If you're happy with pandas, one option is to group the data z

to cut ( pandas.cut

) the x and y data. Then apply average ( .mean()

) and unfasten to get a pivot table.

df.z.groupby([pd.cut(x, bins=xbins), pd.cut(y, bins=ybins)]) \
            .mean().unstack(fill_value=0)

      

Here's a complete example:



import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import  matplotlib.colors

x = np.arange(1,8)
y = np.arange(1,6)
X,Y = np.meshgrid(x,y)

df = pd.DataFrame({"x":X.flatten(), "y":Y.flatten(), "z":(X*Y).flatten()})

xbins = [0,4,8]
ybins = [0,3,6]
hist = df.z.groupby([pd.cut(df.x, bins=xbins), pd.cut(df.y, bins=ybins)]) \
            .mean().unstack(fill_value=0)
print hist
im = plt.imshow(hist.values, norm=matplotlib.colors.LogNorm(1,100))
plt.xticks(range(len(hist.index)), hist.index)
plt.yticks(range(len(hist.columns)), hist.columns)
plt.colorbar(im)
plt.show()

      

The log rate must be manually generated and the ticks must be labeled according to binning.

enter image description here

0


source







All Articles