python matplotlib中的4D绘图

2024-06-12 14:44:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python编程非常陌生,所以我对它有点迷茫。你知道吗

我正在尝试创建一个scatter3D绘图,它为时间位置绘制X、Y、Z坐标,并使用颜色作为4维来绘制值。你知道吗

我面临的问题是。。。对我来说,似乎不可能将变量添加为color并添加colorbar以获得帮助。你知道吗

我在这里解释背景:我已经为X,Y,Z创建了一个网格,以及一个数组,用于计算X,Y,Z坐标在每个位置的值(这是一个三边数组)。你知道吗

它遵循一个循环,用每个X,Y,Z组合的值填充数组,然后我尝试创建这个图。你知道吗

x = np.linspace(0, 15, 15)
y = np.linspace(0, 30, 15)
z = np.linspace(5, 45, 15)

X, Y, Z = np.meshgrid(x, y, z)

sumatorio = np.zeros(shape=(len(X[0, :, 0]), len(Y[:, 0, 0])))

sumatorio_1 = np.zeros(shape=(len(Y[:, 0, 0]), len(Z[0, 0, :])))

sum_total = np.zeros(shape=(len(X[:, 0, 0]), len(Y[0, :, 0]), len(Z[0, 0, 
:])))

c = np.array([])

for i in range(len(X[0, :, 0])):
    for j in range(len(Y[:, 0, 0])):
        sum_1 = X[0, i, 0] + Y[j, 0, 0]
        sumatorio[i, j] = sum_1
            for k in range(len(Z[0, 0, :])):
            sum_2 = sum_1 ** 2.0 + Z[0, 0, k] + X[0, i, 0]
            sumatorio_1[j, k] = sum_2
            sum_total[i, j, k] = sumatorio[i, j] + sumatorio_1[j, k]
fig = plt.figure(figsize=(50.0, 50.0))
ax = fig.add_subplot(111, projection='3d')

ax.scatter3D(X, Y, Z, c=sum_total, cmap='coolwarm', depthshade=0)
fig.colorbar(sum_total)

plt.title("DV at departure from Earth")
ax.set_xlabel("Beginning")
ax.set_ylabel("Time of flight")
ax.set_zlabel("Time of flight 2")

plt.show()

当我执行代码时,会出现以下错误:

'c' argument has 15 elements, which is not acceptable for use with 'x' with size 3375, 'y' with size 3375.

当我把数组的和放在这里时,它似乎不知道它的形状。你知道吗

我尝试将颜色添加为c=np.ravel(sum_total),这将返回带有颜色的图形,但我认为没有为每个点提供适当的颜色。你知道吗

另外,当我用np.ravel(sum_total)创建图形时,颜色栏会给出以下错误:

'numpy.ndarray' object has no attribute 'get_array'


Tags: inforlen颜色npzerosfigrange
1条回答
网友
1楼 · 发布于 2024-06-12 14:44:35

我检查了你的代码,修改了一些东西。你知道吗

首先,我添加了我猜您想要使用的导入:

import matplotlib
matplotlib.use('Agg')

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

我还添加了一些变量,使所有内容都稍微易于阅读,并且不太可能引起错误:

x = np.linspace(0, 15, 15)
y = np.linspace(0, 30, 15)
z = np.linspace(5, 45, 15)

X, Y, Z = np.meshgrid(x, y, z)

lenX = len(X[0, :, 0])
lenY = len(Y[:, 0, 0])
lenZ = len(Z[0, 0, :])

并在代码中实现了这些变量:

sumatorio = np.zeros(shape=(lenX, lenY))
sumatorio_1 = np.zeros(shape=(lenY, lenZ))
sum_total = np. zeros(shape=(lenX, lenY, lenZ))
c = np.array([])

for i in range(lenX):
    for j in range(lenY):
       sum_1 = X[0, i, 0] + Y[j, 0, 0]
       sumatorio[i, j] = sum_1
       sum_1_2 = sum_1 * sum_1

我更改了“k for loop”的缩进:

        for k in range(lenZ):
            sum_2 = sum_1_2 + Z[0, 0, k] + X[0, i, 0]
            sum_total[i, j, k] = sumatorio[i, j] + sumatorio_1[j, k]

fig = plt.figure(figsize=(50.0, 50.0))
ax = fig.add_subplot(111, projection='3d')

ax.scatter3D(X, Y, Z, c=sum_total, cmap='coolwarm', depthshade=0)

那个图色条对我来说没什么意义,我不太确定你希望用这个实现什么,所以我把它注释掉了。你知道吗

#fig.colorbar(sum_total)

plt.title("DV at departure from Earth")
ax.set_xlabel("Beginning")
ax.set_ylabel("Time of flight")
ax.set_zlabel("Time of flight 2")

plt.show()

这个代码对我来说运行得很好。我真的不明白它是干什么的,但我相信你知道得更多。你知道吗

相关问题 更多 >