Python - colormap in matplotlib for 3D graphics

I'm trying to draw a 3D plot with mplot3D matplotlib tools. I have 4 arrays

  • tab_C [0] is an x-value array
  • tab_C [1] - array of y values
  • tab_C [2] - an array of z-values
  • tab_t is an array of time values

I am drawing my plot with this:

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

fig1 = plt.figure()
ax = fig1.gca(projection='3d')
ax.plot(tab_C[0], tab_C[1], tab_C[2])

plt.show()

This works, but now I want this plot to have a rainbow color based on the value of time. I searched matplotlib webpage but nothing. Any suggestion on this issue?

+4
source share
2 answers

, "" . . , , - . :

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

N_points = 10
x = np.arange(N_points, dtype=float)
y = x
z = np.random.rand(N_points)
t = x

fig = plt.figure()
ax = fig.gca(projection='3d')

# colors need to be 3-tuples with values between 0-1.
# if you want to use the time values directly, you could do something like
t /= max(t)
for i in range(1, N_points):
    ax.plot(x[i-1:i+1], y[i-1:i+1], z[i-1:i+1], c=(t[i-1], 0, 0))
plt.show()

enter image description here

. 2 , . .

c = (t[i-1], t[i-1], t[i-1])

:

# Don't do: t /= max(t)
from itertools import cycle
colors = cycle('bgrc')
for i in range(1, N_points):
    ax.plot(x[i-1:i+1], y[i-1:i+1], z[i-1:i+1], c=colors[t[i-1]])
plt.show()

, , .

+3

matploblib, , Mayavi. :

from mayavi import mlab
n_mer, n_long = 6, 11
dphi = np.pi / 1000.0
phi = np.arange(0.0, 2 * pi + 0.5 * dphi, dphi)
mu = phi * n_mer
x = np.cos(mu) * (1 + np.cos(n_long * mu / n_mer) * 0.5)
y = np.sin(mu) * (1 + np.cos(n_long * mu / n_mer) * 0.5)
z = np.sin(n_long * mu / n_mer) * 0.5
t = np.sin(mu)

mlab.plot3d(x, y, z, t, tube_radius=0.025, colormap='Spectral')

enter image description here

colormap colormap x, y, z, t , .

+3

Source: https://habr.com/ru/post/1541629/


All Articles