2

I would like to show a heatmap in any Python library, where color is determined by the mean value of data points in the cell. But additionally, I'd like to visualize the uncertainty of these means, but using the number of data points in each cell as the alpha value. Hence, cells with few data points will have a color, but faded.

How can I do this in Python the easiest way, when the full color is determined by two values?

import pandas as pd
import numpy as np
import seaborn as sns
from operator import attrgetter

N = 10000

D = pd.DataFrame(
    {
        "x": np.random.uniform(size=N),
        "y": np.random.uniform(size=N),
        "c": np.random.choice([0, 1], size=N),
    }
)

x_group = pd.cut(D["x"], 10).apply(attrgetter("left"))
y_group = pd.cut(D["y"], 10).apply(attrgetter("left"))

means = D.pivot_table("c", x_group, y_group, aggfunc="mean")
sizes = D.pivot_table("c", x_group, y_group, aggfunc="size")

sns.heatmap(means)  # here sizes should be used for the alpha channel - maybe in a postprocessing?
Gere
  • 12,075
  • 18
  • 62
  • 94
  • matplotlib has a parameter 'alpha', so I guess you should be using that? That said, I do want to point out 'uncertainty' is not solely depending on number of datapoints, so not sure if you want to go this route. – Chrisvdberge Dec 06 '19 at 11:04
  • That alpha parameter is a single fixed number so it cannot be specified per cell? The uncertainty may be more precise later - here it's just an example. – Gere Dec 06 '19 at 12:15
  • in matplotlib you'll have to work around this by adjusting the colors themselves. You can do this any way you see fit, but for instance; https://stackoverflow.com/questions/24767355/individual-alpha-values-in-scatter-plot – Chrisvdberge Dec 06 '19 at 12:21
  • How I can access and change the colors of my plot for a heatmap? – Gere Dec 06 '19 at 13:15
  • Matplotlib colormaps map a single value to a color. Here you have two values. So you cannot use a colormap, but need to define your own mapping. I.e. you need a function that takes two input values and outputs an RGBA color. (of course this function could make use of colormaps internally for each part) Apply that function to two matrices of the two values to obtain an RGBA image. Plot that image via `plt.imshow`. – ImportanceOfBeingErnest Dec 06 '19 at 13:24

0 Answers0