从三个测量阵列中绘制Python的表面

发布于 2025-02-01 04:01:15 字数 3080 浏览 1 评论 0 原文

您好,我有三个数组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()

“

Hello I have three arrays x, y, and z where i want to view the, magnitude of variable z at the co-ords of x and y. I.e. I want to plot a surface of three [6147,1] arrays. using the code below i get Value Error: cannot reshape array of size 6147 into shape (6147,6147) can anyone trouble shoot this and help me get this visualisation to work?

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()


Photo reference from comment section

Array of z plotted


New code for new context to comments

# -*- 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()

Current Graph

Variable Explorer for variables of interest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2025-02-08 04:01:15

使用 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的索引或多指数用于坐标,因此您需要先设置该索引。

您需要从类似的开始:

import xarray as xr

df = df.set_index(['x', 'y'])
ds = xr.Dataset.from_dataframe(df)
ds['angle'].plot()

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 the xarray.Dataset which is like a collection of DataArray objects that share coordinates.

So you'll either want to use xarray.DataArray.from_series for a single variable (e.g. angle) or xarray.Dataset.from_dataframe for multiple variables (e.g. angle and vel). 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:

import xarray as xr

df = df.set_index(['x', 'y'])
ds = xr.Dataset.from_dataframe(df)
ds['angle'].plot()
只想待在家 2025-02-08 04:01:15

我不确定您的数据到底是什么,因此我无法运行测试用例。但是,这似乎是一个非常标准的情况。假设 z 确实足够长以将其重塑为(6147,6147),则可能只需要

xx, yy = np.meshgrid(np.squeeze(x), np.squeeze(y))
ax.plot_surface(x, y, z.T)

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

xx, yy = np.meshgrid(np.squeeze(x), np.squeeze(y))
ax.plot_surface(x, y, z.T)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文