Home > Blockchain >  How to create a 2d probability density heatmap
How to create a 2d probability density heatmap

Time:08-18

I have x, y coordinates which I want to represent the 2d axis, and then the values for the plot I want to be a heatmap corresponding to probabilities for the respective x,y coordinates. Here's the sample data I have right now.

import numpy as np
import random
import pandas as pd

x = np.linspace(-1,1,200)
y = np.linspace(-1,1,200)
z = [random.uniform(0, 1) for val in range(200)]


df = pd.DataFrame({'x':x,'y':y,'z':z})

I want it to look something like this (obviously with different axes etc):

enter image description here

How would I do this?

CodePudding user response:

A simple example:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(8,8))
im = ax.imshow([[np.random.uniform(0, 1) for _ in range(100)] for _ in range(100)], extent=[-1, 1, -1, 1])
cbar = ax.figure.colorbar(im, ax=ax)

Simple random heatmap

The idea is to have a 2d array of the intensities. This can be done in many ways, such as using histogram functions, or just providing noise as in the example. As in this simple case we know that the x and y values are uniformly spread, we don't have to supply these values, except for in the extent=[..] arg, containing the x and y ranges respectively.

CodePudding user response:

This is an example on the matplotlib gallery page that I won't repeat here...

https://matplotlib.org/stable/plot_types/stats/hist2d.html#sphx-glr-plot-types-stats-hist2d-py

  • Related