简体   繁体   中英

4D plot in python matplotlib

I am quite new with python programming so I am a bit lost with it.

I am trying to create a scatter3D plot which plots X, Y, Z coordinates for time position and use the color as 4 dimension to plot the value.

The problem I am facing is that... it seems impossible to me to add the variable as colour and add a colorbar for help.

I explain the background here: I have created a meshgrid for X, Y, Z and an array for computing the value in each position for X, Y, Z coordinates (which is an trimensional array).

It follows an loop for filling the array with the values for each X,Y,Z combination and then i try to create the graph.

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

When I execute the code, the following error comes into view:

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

It seems when I put the array sum_total here, it did not recognize the shape of it.

I try to add the color as c=np.ravel(sum_total) which returns me the graphic with color, but I think it is not given the appropiate color to each point.

Also, when I create the graphic with np.ravel(sum_total) , the colorbar gives the following error:

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

I've gone over your code, and changed a couple things.

Firstly, I've added the imports I guessed you wanted to use:

import matplotlib
matplotlib.use('Agg')

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

I've also added some variables to make everything slightly easier to read and less likely to cause bugs:

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, :])

And implemented these variables in the code:

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

I changed the indentation of the '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)

The fig.colorbar didn't really make sense to me, I'm not quite sure what you're hoping to achieve with this, so I commented it out.

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

This code runs fine for me. I don't really understand what it does, but I'm sure you know more about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM