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

时间:2015-07-01 11:20:23

标签: python python-2.7 matplotlib plot

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

这是我目前正在使用的代码:

CREATE VIEW R1
AS SELECT v.c1 as c1, v.c2 as c2, ... p.c1 as cx, p.c2 as cy ...
FROM Table1 v
  JOIN Table2 p ON v.V# = p.V#;

这给了我一个奇怪的结果(http://i.stack.imgur.com/7fLeT.png),我不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

您可能不希望将2D绘图中的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