从两个向量和一个数组的3d图

时间:2017-03-28 15:45:27

标签: python python-3.x plot

我有两个向量存储我的X,Y值而不是长度81,105然后是一个(81,105)数组(实际上是一个列表列表),它存储我的那些X,Y的Z值。什么是最好的在3d中绘制这个的方法?这就是我尝试过的:

Z = np.load('Z.npy')
X = np.load('X.npy')
Y = np.linspace(0, 5, 105)
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap= 'viridis')
plt.show() 

我收到以下错误:ValueError:形状不匹配:对象无法广播到单个形状

1 个答案:

答案 0 :(得分:1)

好的,我让它运行了。这里有一些技巧。我会在代码中提到它们。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from random import shuffle

# produce some data.
x = np.linspace(0,1,81)
y = np.linspace(0,1,105)
z = [[i for i in range(81)] for x in range(105)]
array_z = np.array(z)

# Make them randomized.
shuffle(x)
shuffle(y)
shuffle(z)

# Match data in x and y.
data = []
for i in range(len(x)):
    for j in range(len(y)):
        data.append([x[i], y[j], array_z[j][i]])
        # Be careful how you data is stored in your Z array. 
# Stored in dataframe   
results = pd.DataFrame(data, columns = ['x','y','z'])

# Plot the data.    
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(results.x, results.y, results.z, cmap= 'viridis')

enter image description here

图片看起来很奇怪,因为我生成了一些数据。希望它有所帮助。