Reputation: 12627
My data data
is a 3-dimensional matrix, of the shape X, Y, Z
.
I want to plot something that is visually like a 2d distribution plot, e.g:
Where the location of each square is given by X
and Y
but the color (hue and intensity) is given by the average value of Z
of the points within the square regardless of the density.
How can I achieve that with seaborn?
Using a scatter plot with hue does not serve me as many points overlay and I want to see the average of all the points within an area
Upvotes: 0
Views: 528
Reputation: 10545
You can use sns.heatmap()
, but it is a bit tricky to get the data in the right shape. Here is one way to do it, using df.round()
to bin the points into 11x11 tiles.
import numpy as np
import pandas as pd
import seaborn as sns
n = 10_000 # number of points
data = np.random.uniform(size=(n, 3))
df = pd.DataFrame(data, columns=['X', 'Y', 'Z'])
df[['X', 'Y']] = df[['X', 'Y']].round(1)
tiles = df.groupby(['X', 'Y']).mean()
tile_matrix = np.zeros(shape=(11, 11))
for x, y, z in tiles.reset_index().values:
tile_matrix[int(10 * x), int(10 * y)] = z
sns.heatmap(tile_matrix, cmap='Blues');
Upvotes: 1