从三个测量阵列中绘制Python的表面
您好,我有三个数组x,y和z,我想在x和y的坐标处查看可变z的大小。即,我想绘制三个[6147,1]阵列的表面。使用下面的代码,我会得到价值错误:无法将大小6147的阵列重塑为形状(6147,6147),任何人都可以麻烦地拍摄并帮助我使此可视化工作吗?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
data_frame = pd.read_excel (r'\Users\alexd\OneDrive\Documents\University\Monash Motor Sport\Junior Project\Junior Project\MotionControlData.xlsx') #place "r" before the path string to address special character, such as '\'. Don't forget to put the file name at the end of the path + '.xlsx'
df_x = pd.DataFrame(data_frame, columns= ['x'])
df_y = pd.DataFrame(data_frame, columns= ['y'])
df_angle = pd.DataFrame(data_frame, columns= ['angle'])
df_vel = pd.DataFrame(data_frame, columns= ['vel'])
# Creating dataset
x = np.array(df_x)
y = np.array(df_y)
angle = np.array(df_angle)
vel = np.array(df_vel)
z = vel
x = np.reshape(x, (6147, 6147))
y = np.reshape(y, (6147, 6147))
z = np.reshape(z, (6147, 6147))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
新上下文的新代码注释
# -*- coding: utf-8 -*-
"""
Created on Fri May 13 18:30:41 2022
@author: alexd
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
data_frame = pd.read_excel (r'\Users\alexd\OneDrive\Documents\University\Monash Motor Sport\Junior Project\Junior Project\MotionControlData.xlsx') #place "r" before the path string to address special character, such as '\'. Don't forget to put the file name at the end of the path + '.xlsx'
df_x = pd.DataFrame(data_frame, columns= ['x'])
df_y = pd.DataFrame(data_frame, columns= ['y'])
df_angle = pd.DataFrame(data_frame, columns= ['angle'])
df_vel = pd.DataFrame(data_frame, columns= ['vel'])
# Creating dataset
x = np.array(df_x)
y = np.array(df_y)
angle = np.array(df_angle)
vel = np.array(df_vel)
z = vel
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xx, yy = np.meshgrid(np.squeeze(x), np.squeeze(y))
ax.plot_surface(x, y, z.T, cmap=cm.coolwarm,
linewidth=1, antialiased=False)
ax.set_xlabel('X ')
ax.set_ylabel('Y ')
ax.set_zlabel('Velocity ')
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
xarray
,因为它可以为您构建Numpy数组,而无需担心您在数据中读取的顺序或数据集的形状。简而言之,
xarray
为n二维numpy阵列(除了此外)提供类似熊猫的索引。因此,例如,您可以为2-D Numpy数组具有地理(X,Y)坐标,而不必使用Numpy的0基整数索引。有两个主要数据结构:
xarray.dataArray
,基本上就像一个numpy数组,而xarray.dataset
,就像dataArray的集合
/代码>共享坐标的对象。因此,您要么要使用 。 .from_dataframe.html“ rel =“ nofollow noreferrer”>
xarray.dataset.from_dataframe
对于多个变量)。在每种情况下,dataFrame的索引或多指数用于坐标,因此您需要先设置该索引。您需要从类似的开始:
It's much easier to do this kind of thing with
xarray
, because it can construct the NumPy array for you without you needing to worry about the order in which you read in the data, or the shape of the dataset.In a nutshell,
xarray
gives you Pandas-like indexing for n-dimensional NumPy arrays (and a lot more besides). So, for instance, you can have geographic (x, y) coordinates for your 2-D NumPy array, instead of having to use NumPy's 0-based integer indices.There are two main data structures: the
xarray.DataArray
, which is basically like a NumPy array, and thexarray.Dataset
which is like a collection ofDataArray
objects that share coordinates.So you'll either want to use
xarray.DataArray.from_series
for a single variable (e.g.angle
) orxarray.Dataset.from_dataframe
for multiple variables (e.g.angle
andvel
). In each case, the dataframe's index or multi-index is used for the coordinate(s), so you'll want to set that first.You'll want to start with something like:
我不确定您的数据到底是什么,因此我无法运行测试用例。但是,这似乎是一个非常标准的情况。假设
z
确实足够长以将其重塑为(6147,6147),则可能只需要I am not sure what your data exactly are, so I cannot run a test case. However it seems a pretty standard case. Assuming
z
is indeed long enough to be reshaped into (6147, 6147), you probably need just