简体   繁体   English

如何创建二维概率密度热图

[英]How to create a 2d probability density heatmap

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.我有 x,y 坐标,我想代表 2d 轴,然后是 plot 的值,我想成为对应于各个 x,y 坐标概率的热图。 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):我希望它看起来像这样(显然有不同的轴等):

在此处输入图像描述

How would I do this?我该怎么做?

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)

简单的随机热图

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.在这个简单的例子中,我们知道xy值是均匀分布的,我们不必提供这些值,除了在extent=[..] arg 中,分别包含xy范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM