如何将散点图转换为曲面图?

2024-09-30 16:33:28 发布

您现在位置:Python中文网/ 问答频道 /正文

初学者使用python,我有一个散点图(http://i.stack.imgur.com/sQNHM.png)。我要做的是生成一个3D图,显示这些点的Z方向上的尖峰,其他地方都是0。在

这是我当前使用的代码:

plt.scatter(X, Y) 
plt.show()

X, Y = np.meshgrid(X, Y)
Z = [1] * len(X)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.plot_surface(X, Y, Z)
plt.show()

这给了我一个奇怪的结果(http://i.stack.imgur.com/7fLeT.png),我不知道该怎么做才能修复它。在


Tags: comhttppngstackshow地方figplt
1条回答
网友
1楼 · 发布于 2024-09-30 16:33:28

您可能不想使用二维绘图中的x和y值作为meshgrid的输入,因为您希望为您范围内x和y的所有整数值定义此绘图。如果我正确理解你的问题,原始的x和y应该定义尖峰的位置。以下是一种在指定位置获得高度为100的3D绘图的方法:

from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Create X, Y and Z arrays
x = range(0,250)
y = range(0,250)
X, Y = np.meshgrid(x, y)
Z = np.zeros((250,250))
# Locations of the spikes. These are some made up numbers. 
dataX = np.array([25,80,90,145,180])
dataY = np.array([170,32,130,10,88])
# Set spikes to 100
Z[dataX,dataY] = 100
# Plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.plot_surface(X, Y, Z)
plt.show()

enter image description here

相关问题 更多 >