Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - colormap in matplotlib for 3D line plot

I'm trying to draw a 3D line plot with the toolkits mplot3D of matplotlib I have 4 arrays

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

I've draw 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()

It works but now I want this plot have rainbow coloring based on the time-value. I've searched the web page of matplotlib but there's nothing. Any suggestion on this problem?

like image 392
pomxipum Avatar asked May 22 '14 14:05

pomxipum


People also ask

How do I draw a 3D line in Matplotlib?

A three-dimensional axes can be created by passing the keyword projection='3d' to any of the normal axes creation routines. We can now plot a variety of three-dimensional plot types. The most basic three-dimensional plot is a 3D line plot created from sets of (x, y, z) triples. This can be created using the ax.

Can Matplotlib Pyplot be used to display 3D plots?

Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit.

How do I plot 3D in Matplotlib?

The 3D plotting in Matplotlib can be done by enabling the utility toolkit. The utility toolkit can be enabled by importing the mplot3d library, which comes with your standard Matplotlib installation via pip.

How do you plot a colormap in Python?

Get the hue colormap, defining a palette. Plot x, y and z data points using scatter () method. Place a legend on the plot. To display the figure, use show () method.

How to create contour plot in Matplotlib?

Contour plot is a way of showing a 3D graph by plotting constant z-slices. Filled contour fills the areas that were shown by the line in contour plots. The first step is to import all the necessary packages for plotting the above plot. Apart from matplotlib.pyplot and NumPy, we are importing another package which is mpl_toolkits.mplot3d.

How do you represent color in Matplotlib?

One way to represent color is using CIELAB. In CIELAB, color space is represented by lightness, L ∗; red-green, a ∗; and yellow-blue, b ∗. The lightness parameter L ∗ can then be used to learn more about how the matplotlib colormaps will be perceived by viewers.


1 Answers

You can do it in pure matploblib fashion as Bill shows, but it is more intuitive with Mayavi. Here is a nice example from their documentation:

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

It is just the argument colormap that determines the colormap, and x, y, z, t can be replaced by the particular array that you want.

like image 156
oschoudhury Avatar answered Sep 21 '22 17:09

oschoudhury