python - 如何在彼此之上绘制 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)
解决方案
推荐阅读
- python - 引发子异常类型时不会捕获父异常类型
- python - 如何在 Tkinter 中输入时间并使用它来运行代码?
- oracle - 选择值的日期列的记录(查询执行天+ 2天)
- go - 如何从 tls.Certificate 中获取 SubjectKeyId?
- apache-kafka - Kafka Streams Applications - 我们可以在一个微服务中拥有 2 个流应用程序吗?
- c# - 使用窗口形式c#中的委托传递数据(订阅和取消订阅)
- git - 从我当地的主人那里挑选上一个提交的最佳方法
- c++ - 如何在 shell 环境(winapi)中获取带扩展名的文件名?
- openedge - Openedge 在现有文本文件的开头添加文本
- react-native - HitRect 如何在 React Native 中与 Pressable 一起使用?