bluesummers
bluesummers

Reputation: 12627

Seaborn plot a distribution plot where the color is the average value of a 3rd dimension

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:

enter image description here

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

Answers (1)

Arne
Arne

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');

heatmap example

Upvotes: 1

Related Questions