首页 > 解决方案 > 如何在彼此之上绘制 3D 图并在 python 中突出显示它们的交集

问题描述

我有 2 个如下所示的数据框:

import random
import numpy as np
import pandas as pd
A = pd.DataFrame({'x':[random.uniform(0, 1) for i in range(0,100)], 'y':[random.uniform(0, 1) for i in range(0,100)], 
                  'z':[random.uniform(0, 1) for i in range(0,100)], 'w':[random.uniform(0, 1) for i in range(0,100)]})
B = pd.DataFrame({'x':[random.uniform(0, 1) for i in range(0,100)], 'y':[random.uniform(0, 1) for i in range(0,100)], 
                  'z':[random.uniform(0, 1) for i in range(0,100)], 'w':[random.uniform(0, 1) for i in range(0,100)]})

从使用此函数的这两个数据帧中:

import matplotlib.pyplot as plt
import scipy.stats as st

def plot_2d_kde(df):
    # Extract x and y
    x = df['x']
    y = df['y']
    # Define the borders
    deltaX = (max(x) - min(x))/10
    deltaY = (max(y) - min(y))/10
    xmin = min(x) - deltaX
    xmax = max(x) + deltaX
    ymin = min(y) - deltaY
    ymax = max(y) + deltaY

    # Create meshgrid
    xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]

    # We will fit a gaussian kernel using the scipy’s gaussian_kde method
    positions = np.vstack([xx.ravel(), yy.ravel()])
    values = np.vstack([x, y])
    kernel = st.gaussian_kde(values)
    f = np.reshape(kernel(positions).T, xx.shape)

    fig = plt.figure(figsize=(13, 7))
    ax = plt.axes(projection='3d')
    surf = ax.plot_surface(xx, yy, f, rstride=1, cstride=1, cmap='coolwarm', edgecolor='none')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('PDF')
    ax.set_title('Surface plot of Gaussian 2D KDE')
    fig.colorbar(surf, shrink=0.5, aspect=5) # add color bar indicating the PDF
    ax.view_init(60, 35)

我可以制作这两个地块

plot_2d_kde(A)

在此处输入图像描述 plot_2d_kde(B) 在此处输入图像描述 有没有办法将这两个图绘制在一起并突出显示它们的交集?交叉点是指两个图共同的表面

标签: pythonpython-3.xmatplotlibseaborn

解决方案


推荐阅读