修复丑陋的表面图

时间:2015-05-05 09:12:29

标签: python python-2.7 matplotlib plot

我正在尝试将2d数组表示为曲面图。例如,给出以下数据:

data = [[88873.0], [107535.0], [27428.0], [1360.0], [12310.0]]

我得到了下图:

enter image description here

有更多数据

data = [[88873.0], [107535.0], [27428.0], [1360.0], [12310.0], [0], [106113.0, 96156.0], [0], [102891.0], [21726.0]]

我明白了:

enter image description here

我们可以看到顶部的曲线是如何丢失的,给人的印象是有几个峰值。我使用的数据越多,它就越糟糕。

表面绘图代码:

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

arr = data
new_arr = []

N= 2 //maximum number of values in Z

for i, sub_arr in enumerate(arr):
  new_arr.append((i+1, -1, 0))
  new_arr.append((i+1, N, 0))
  for j, z in enumerate(sub_arr):
    new_arr.append((i+1, j, z))
    if (len(sub_arr) < N) & (j == N-2):
    new_arr.append((i+1, j+1, 0))
x, y, z = zip(*new_arr)
z = map(float, z)
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic')

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(grid_x, grid_y, grid_z, cmap=plt.cm.Spectral)
plt.show()

你知道怎么做一个更有代表性的人物吗?

0 个答案:

没有答案